Ask a Question related to Mac Programming, Design and Development.
-
Steve Ketcham #1
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 documentation 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
documented?
(Or is this not possible? I note that the GNUstep documentation says
that the key must be an NSString. Apple's documentation just says that
the key is an object.)
Thanks,
Steve
Steve Ketcham Guest
-
Determining all shortcut keys in use (including Function keys)?
I'm creating a Windows Acrobat 8 plug-in and would like to know how to find all the current shortcut keys. The Acrobat 8 SDK's Developing Plug-ins... -
Access keys
During a personnel change the contribute administrator has left. I need to know how to get Administrator Access and access to the installation key.... -
Using Keyboard keys
I want to use either the shift key, space key or control key to control a character in my 3D world. I have already got my character moving forward,... -
encryption and it's keys
HI, I'm after some general pointers about best practice. I've used TripleDES to encrypt the connection string of a web app. How do I go about... -
#21788 [Sus->WFx]: array_multisort() changes array keys unexpectedly given numeric strings as keys
ID: 21788 Updated by: sniper@php.net Reported By: jon at inet-specialists dot com -Status: Suspended +Status: ... -
Tom Harrington #2
Re: Keys in an NSDictionary
In article <160920031326401042%sgketcham@comcast.net>,
Steve Ketcham <sgketcham@comcast.net> wrote:
The docs for -[NSObject isEqual] note that:> 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 documentation 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
> documented?
>
> (Or is this not possible? I note that the GNUstep documentation says
> that the key must be an NSString. Apple's documentation just says that
> the key is an object.)
"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
-
matt neuburg #3
Re: Keys in an NSDictionary
In <tph-ABDB93.12000616092003@localhost> Tom Harrington wrote:
He just told you he did. :) Okay, I haven't tried this, but I find the> In article <160920031326401042%sgketcham@comcast.net>,
> Steve Ketcham <sgketcham@comcast.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?
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]matt@tidbits.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
-
Michael Ash #4
Re: Keys in an NSDictionary
In article <160920031326401042%sgketcham@comcast.net>,
Steve Ketcham <sgketcham@comcast.net> wrote:
According to the NSDictionary docs:> 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 documentation 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
> documented?
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
-
Steve Ketcham #5
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
-
Michael Ash #6
Re: Keys in an NSDictionary
In article <160920031536570101%sgketcham@comcast.net>,
Steve Ketcham <sgketcham@comcast.net> wrote:
Did you look at NSValue? You can use that to store ints, and NSValue> 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.
objects work as keys to NSDictionary. Of course you don't have all the
fun of implementing your own class.
What do you mean by "hung"? Did you poke around with the debugger?> 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.
Have a look at> Michael asked if I'd subclassed NSString "in a special way." I didn't
> -- what is the special way.
<[url]http://developer.apple.com/documentation/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
-
Tom Harrington #7
Re: Keys in an NSDictionary
In article <20030916111155552-0700@news.la.sbcglobal.net>,
matt neuburg <matt@tidbits.com> wrote:
D'oh. That's what I get for posting before I've had enough coffee.> In <tph-ABDB93.12000616092003@localhost> Tom Harrington wrote:>> > In article <160920031326401042%sgketcham@comcast.net>,
> > Steve Ketcham <sgketcham@comcast.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.
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
-
Steve Ketcham #8
Re: Keys in an NSDictionary
In article <mail-D14D9F.21472216092003@localhost>, Michael Ash
<mail@mikeash.com> wrote:
Yes, NSValue would work. It was more of an exercise to learn about> 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.
keys. By the way, implementing the copyWithZone: solved the problem
I meant that I attempted to step through the call to setObject:forKey:> What do you mean by "hung"? Did you poke around with the debugger?
and it never returned to the debugger. I didn't poke further.Thanks for this. I'll read it.>>> > 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/documentation/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.
S
Steve Ketcham Guest
-
matt neuburg #9
Re: Keys in an NSDictionary
In <160920031923335824%sgketcham@comcast.net> Steve Ketcham wrote:
You're welcome. m.> implementing the copyWithZone: solved the problem
--
matt neuburg, phd = [email]matt@tidbits.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



Reply With Quote

