Re: NSDictionary trouble
Re: NSDictionary trouble
- Subject: Re: NSDictionary trouble
- From: Ken Thomases <email@hidden>
- Date: Wed, 20 Jan 2010 05:36:09 -0600
On Jan 20, 2010, at 5:22 AM, Jens Miltner wrote:
> Am 20.01.2010 um 11:34 schrieb Jeremy Pereira:
>
>> On 19 Jan 2010, at 23:06, Jens Alfke wrote:
>>
>>> On Jan 19, 2010, at 10:46 AM, Kirk Kerekes wrote:
>>>
>>>> NSDictionary will use almost any object as a key:
>>>>
>>>> From the docs:
>>>> "In general, a key can be any object (provided that it conforms to the NSCopying protocol...)"
>>>>
>>>> -- and if it is an immutable object, that -copy is just a -retain.
>>>
>>> Yes, but he said he wants to use a view as the key. Views are not immutable, and do copy themselves in response to -copy. Having a dictionary whose key objects are copies of your live views is not very useful. (Especially since the copied view will have a different hashcode from the original one, so you can't even look it up using the original view anymore...)
>>
>> Well he could write a category to give him an object that satisfies all the criteria e.g (written in Mail).
>>
>>
>> @interface NSView(DictionaryKey)
>>
>> -(NSNumber*) dictionaryKey;
>>
>> @end
>>
>> @implementation NSView(DictionaryKey)
>>
>> -(NSNumber*) dictionaryKey
>> {
>> return [NSNumber numberWithUnsignedLongLong: (unsigned long long) self];
>> }
>
> Slightly safer (because it does not make explicit assumptions about pointer sizes):
>
> -(id) dictionaryKey
> {
> return [NSString stringWithFormat:@"%p", self];
> }
>
>
> Sorry, couldn't resist ;-)
Um, if you're going to go that route, use +[NSValue valueWithNonretainedObject:]. Playing with strings or NSNumbers is doing it the hard way. But, if you can use it, NSMapTable (as mentioned previously) is probably more appropriate.
Regards,
Ken
_______________________________________________
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