Professional Web Applications Themes

Keys in an NSDictionary - Mac Programming

I'd like to implement my own class for use as keys in an NSDictionary. My first try was to subclass NSObject and implement isEqual: and hash, which is what the NSDictionary doentation implies is necessary. When that didn't work, I subclassed NSString, but had problems initializing super with the text I wanted. Does anyone know what the requirements are, or where they might be doented? (Or is this not possible? I note that the GNUstep doentation says that the key must be an NSString. Apple's doentation just says that the key is an object.) Thanks, Steve...

  1. #1

    Default Keys in an NSDictionary

    I'd like to implement my own class for use as keys in an NSDictionary.
    My first try was to subclass NSObject and implement isEqual: and hash,
    which is what the NSDictionary doentation implies is necessary.

    When that didn't work, I subclassed NSString, but had problems
    initializing super with the text I wanted.

    Does anyone know what the requirements are, or where they might be
    doented?

    (Or is this not possible? I note that the GNUstep doentation says
    that the key must be an NSString. Apple's doentation just says that
    the key is an object.)

    Thanks,
    Steve
    Steve Ketcham Guest

  2. #2

    Default Re: Keys in an NSDictionary

    In article <160920031326401042%sgketchamcomcast.net>,
    Steve Ketcham <sgketchamcomcast.net> wrote:
    > I'd like to implement my own class for use as keys in an NSDictionary.
    > My first try was to subclass NSObject and implement isEqual: and hash,
    > which is what the NSDictionary doentation implies is necessary.
    >
    > When that didn't work, I subclassed NSString, but had problems
    > initializing super with the text I wanted.
    >
    > Does anyone know what the requirements are, or where they might be
    > doented?
    >
    > (Or is this not possible? I note that the GNUstep doentation says
    > that the key must be an NSString. Apple's doentation just says that
    > the key is an object.)
    The docs for -[NSObject isEqual] note that:

    "If two objects are equal, they must have the same hash value. This
    last point is particularly important if you define isEqual: in a
    subclass and intend to put instances of that subclass into a
    collection. Make sure you also define hash in your subclass."

    Did you implement -hash?

    --
    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 1.4: Best cleanup yet, gets files other tools miss.
    See [url]http://www.atomicbird.com/[/url]
    Tom Harrington Guest

  3. #3

    Default Re: Keys in an NSDictionary

    In <tph-ABDB93.12000616092003localhost> Tom Harrington wrote:
    > In article <160920031326401042%sgketchamcomcast.net>,
    > Steve Ketcham <sgketchamcomcast.net> wrote:
    >
    >> I'd like to implement my own class for use as keys in an NSDictionary.
    >> My first try was to subclass NSObject and implement isEqual: and hash,
    >
    > Did you implement -hash?
    He just told you he did. :) Okay, I haven't tried this, but I find the
    problem interesting. What didn't work about your first attempt? Did you
    remember to make the key class implement NSCopying? m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  4. #4

    Default Re: Keys in an NSDictionary

    In article <160920031326401042%sgketchamcomcast.net>,
    Steve Ketcham <sgketchamcomcast.net> wrote:
    > I'd like to implement my own class for use as keys in an NSDictionary.
    > My first try was to subclass NSObject and implement isEqual: and hash,
    > which is what the NSDictionary doentation implies is necessary.
    >
    > When that didn't work, I subclassed NSString, but had problems
    > initializing super with the text I wanted.
    >
    > Does anyone know what the requirements are, or where they might be
    > doented?
    According to the NSDictionary docs:

    Methods that add entries to dictionaries‹whether as part of
    initialization (for all dictionaries) or during modification (for
    mutable dictionaries)‹copy each key argument (keys must conform to
    the NSCopying protocol) and add the copies to the dictionary.

    So your objects must implement NSCopying. This is a small part of the
    docs and easy to miss but if your objects don't do it, you'll have all
    kinds of problems.

    On a side note, you are properly subclassing NSString (since it's a
    class cluster you have to subclass it in a special way), right?
    Michael Ash Guest

  5. #5

    Default Re: Keys in an NSDictionary

    I'm going to reply to all three of you in one message:

    The dictionary keys will consist of (i,j) indices into a
    two-dimensional array. I could convert them into a string "iijj" and
    use that, but I thought it would be more interesting (ie a better
    learning experience) to keep them as ints and add convenience
    functions to retrieve them, so I don't have to decode the string over
    and over.

    I didn't implement NSCopying, and that's probably my problem. But let
    me answer the questions anyway.

    Tom asked if implemented -hash. I did, by creating the NSString as
    described above, and using its hash value.

    Matt asked what didn't work. The program hung at the addition of the
    key to the dictionary. (It's really a mutable dictionary, of course.)
    Since that's when the key copy is done, it makes sense that copying is
    the problem.

    Michael asked if I'd subclassed NSString "in a special way." I didn't
    -- what is the special way.

    Thanks to all of you,
    Steve
    Steve Ketcham Guest

  6. #6

    Default Re: Keys in an NSDictionary

    In article <160920031536570101%sgketchamcomcast.net>,
    Steve Ketcham <sgketchamcomcast.net> wrote:
    > I'm going to reply to all three of you in one message:
    >
    > The dictionary keys will consist of (i,j) indices into a
    > two-dimensional array. I could convert them into a string "iijj" and
    > use that, but I thought it would be more interesting (ie a better
    > learning experience) to keep them as ints and add convenience
    > functions to retrieve them, so I don't have to decode the string over
    > and over.
    Did you look at NSValue? You can use that to store ints, and NSValue
    objects work as keys to NSDictionary. Of course you don't have all the
    fun of implementing your own class.
    > Matt asked what didn't work. The program hung at the addition of the
    > key to the dictionary. (It's really a mutable dictionary, of course.)
    > Since that's when the key copy is done, it makes sense that copying is
    > the problem.
    What do you mean by "hung"? Did you poke around with the debugger?
    > Michael asked if I'd subclassed NSString "in a special way." I didn't
    > -- what is the special way.
    Have a look at
    <[url]http://developer.apple.com/doentation/Cocoa/Conceptual/Foundation/Con[/url]
    cepts/ClassClusters.html>.

    To summarize: Several Cocoa classes are actually class clusters. What
    this means is that, for example, NSString is just an abstract class.
    When you do [[NSString alloc] init], the object you get is not an
    instance of NSString, but an instance of a private subclass thereof. So
    there are special considerations when you subclass these classes. You
    can't just subclass and override what you feel like, there are specific
    methods that must be implemented to make the thing work at all.
    Michael Ash Guest

  7. #7

    Default Re: Keys in an NSDictionary

    In article <20030916111155552-0700news.la.sbcglobal.net>,
    matt neuburg <matttidbits.com> wrote:
    > In <tph-ABDB93.12000616092003localhost> Tom Harrington wrote:
    > > In article <160920031326401042%sgketchamcomcast.net>,
    > > Steve Ketcham <sgketchamcomcast.net> wrote:
    > >
    > >> I'd like to implement my own class for use as keys in an NSDictionary.
    > >> My first try was to subclass NSObject and implement isEqual: and hash,
    > >
    > > Did you implement -hash?
    >
    > He just told you he did. :) Okay, I haven't tried this, but I find the
    > problem interesting. What didn't work about your first attempt? Did you
    > remember to make the key class implement NSCopying? m.
    D'oh. That's what I get for posting before I've had enough coffee.
    Nevermind.

    --
    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 1.4: Best cleanup yet, gets files other tools miss.
    See [url]http://www.atomicbird.com/[/url]
    Tom Harrington Guest

  8. #8

    Default Re: Keys in an NSDictionary

    In article <mail-D14D9F.21472216092003localhost>, Michael Ash
    <mailmikeash.com> wrote:
    > Did you look at NSValue? You can use that to store ints, and NSValue
    > objects work as keys to NSDictionary. Of course you don't have all the
    > fun of implementing your own class.
    Yes, NSValue would work. It was more of an exercise to learn about
    keys. By the way, implementing the copyWithZone: solved the problem
    > What do you mean by "hung"? Did you poke around with the debugger?
    I meant that I attempted to step through the call to setObject:forKey:
    and it never returned to the debugger. I didn't poke further.
    >
    > > Michael asked if I'd subclassed NSString "in a special way." I didn't
    > > -- what is the special way.
    >
    > Have a look at
    > <[url]http://developer.apple.com/doentation/Cocoa/Conceptual/Foundation/Con[/url]
    > cepts/ClassClusters.html>.
    >
    > To summarize: Several Cocoa classes are actually class clusters. What
    > this means is that, for example, NSString is just an abstract class.
    > When you do [[NSString alloc] init], the object you get is not an
    > instance of NSString, but an instance of a private subclass thereof. So
    > there are special considerations when you subclass these classes. You
    > can't just subclass and override what you feel like, there are specific
    > methods that must be implemented to make the thing work at all.
    Thanks for this. I'll read it.

    S
    Steve Ketcham Guest

  9. #9

    Default Re: Keys in an NSDictionary

    In <160920031923335824%sgketchamcomcast.net> Steve Ketcham wrote:
    > implementing the copyWithZone: solved the problem
    You're welcome. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

Similar Threads

  1. Determining all shortcut keys in use (including Function keys)?
    By Jeff_Bowell@adobeforums.com in forum Adobe Acrobat SDK
    Replies: 1
    Last Post: December 31st, 07:01 PM
  2. Bests keys to use?
    By necromanthus in forum Macromedia Director 3D
    Replies: 2
    Last Post: September 10th, 05:58 PM
  3. Replies: 0
    Last Post: July 16th, 12:52 AM
  4. Shortcut keys
    By krammai in forum Adobe Photoshop Elements
    Replies: 2
    Last Post: July 4th, 08:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139