Re: NSDictionary key types
Re: NSDictionary key types
- Subject: Re: NSDictionary key types
- From: Jon Sigman <email@hidden>
- Date: Thu, 6 Jan 2011 12:50:54 -0800 (PST)
So are the following two equivalent?
id myKey = [NSNumber numberWithInt:8760];
if (myObj = [myDict objectForKey:myKey]) {
. . . do something
}
and
id myKey = @"8760";
if (myObj = [myDict objectForKey:myKey]) {
. . . do something
}
________________________________
From: Nick Zitzmann <email@hidden>
To: Jon Sigman <email@hidden>
Cc: email@hidden
Sent: Thu, January 6, 2011 12:40:00 PM
Subject: Re: NSDictionary key types
On Jan 6, 2011, at 1:10 PM, Jon Sigman wrote:
> I'm a bit new to Cocoa, and I'm trying to get my head around NSDictionary. In
> the Apple documentation it says, "Each entry consists of one object that
> represents the key and a second object that is the key's value. ... In general,
>
> a key can be any object, but note that when using key-value coding the key must
>
> be a string."
>
> So, I supply an object that *represents* the key, meaning that this object
> itself is not the key, but it's value is?
Correct, because the key object is copied rather than retained.
> That makes sense for NSStrings, but
> not if the key is some other object without an obvious "value", like an
>NSArray.
>
> Are the two key types somehow treated differently?
They're copied, too. But in general, keys tend to be either strings or numbers
or URLs or something like that. It is very unusual to have a collection as a
key. As an object, yes, but not as a key.
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden