Re: Dictionary keyed by a few sparse integers?
Re: Dictionary keyed by a few sparse integers?
- Subject: Re: Dictionary keyed by a few sparse integers?
- From: Alex Kac <email@hidden>
- Date: Fri, 29 Oct 2010 09:29:39 -0500
Another way to do it is to use CFDictionaryRef and use the integers directly.
NSInteger type;
dateFormatterStorage = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, 0, &kCFTypeDictionaryValueCallBacks );
formatter = (NSDateFormatter*)CFDictionaryGetValue( dateFormatterStorage, (void*)type);
CFDictionarySetValue( dateFormatterStorage, (void*)type, formatter );
Perfectly legal and something recommended to us at one of the performance talks at WWDC.
On Oct 29, 2010, at 6:29 AM, Graham Cox wrote:
> I have a need to associate objects with an arbitrary unsigned integer, which can be pretty much anything. I figured a dictionary with NSNumbers as keys would be perfect, where the NSNumber is created using +numberWithUnsignedInteger:
>
> Trouble is, each time I create the number, it's a new object even if it contains a value NSNumber has seen before, so the lookup fails.
>
> So, I thought I'd just create suitable static NSNumbers, but I have the same problem in another place, since I then need to know which static NSNumbers I've allocated already to a given integer, which means looking them up. While I could just add them to an array and conduct a linear search - for the current situation efficiency isn't likely to be an issue - this solution wouldn't scale very well if it ever needed to.
>
> Is there an obvious alternative I'm overlooking?
>
> --Graham
>
>
> _______________________________________________
>
> 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
Alex Kac - President and Founder
Web Information Solutions, Inc.
"Champions aren't made in the gyms. Champions are made from something they have deep inside of them - a desire, a dream, a vision. They have last-minute stamina, they have to be a little faster, they have to have the skill, and the will. But the will must be stronger than the skill."
-- Muhammad Ali
_______________________________________________
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