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: Roland King <email@hidden>
- Date: Fri, 29 Oct 2010 19:44:26 +0800
It should work the way you've done it in a NSDictionary.
Yes the numbers created by +numberWithUnsignedInteger: are distinct objects (normally, if I use low numbers they actually aren't, one of Cocoa's little optimizations I guess), however they compare as equal with isEqual: or isEqualToNumber:
NSDictionary uses isEqual: so it should find it with no issue. I just threw together a 10 line program which does that, puts an entry into a dictionary keyed on an NSNumber representation of an NSUInteger and gets it out with another distinct NSNumber of the same NSUInteger, it finds it. I did also test in that case that the two NSNumbers were distinct, they were.
On 29-Oct-2010, at 7:29 PM, 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
_______________________________________________
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