Re: Integer as key in NSMutableDictionary
Re: Integer as key in NSMutableDictionary
- Subject: Re: Integer as key in NSMutableDictionary
- From: Martin Wierschin <email@hidden>
- Date: Mon, 4 May 2009 15:19:50 -0700
Assume that:
NSMutableDictionary *result = [[NSMutableDictionary alloc]
initWithCapacity:10];
NSInteger ID;
And I add objects to the dictionary:
[result setObject:[NSArray arrayWithObjects: {... objects ...}
nil]
forKey:ID];
I am getting warnings when adding integers in the array and assigning
the integer ID as a key.
The Cocoa collection classes (eg: NSDictionary, NSArray, etc) always
require object values, so you'll have to wrap your integer in an
NSNumber like so:
NSNumber* key = [NSNumber numberWithInteger:ID];
[result setObject:whatever forKey:key];
Or, if you don't otherwise need to use the key, you can shorten that to:
[result setObject:whatever forKey:[NSNumber numberWithInteger:ID]];
~Martin
_______________________________________________
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