NSDictionary with integer as number
I have a NSDictionary with the following layout:
{
1:{
... some d开发者_运维问答ata ...
}
...
}
I have a NSNumber object with a integer value of 1, but when I do
[my_dict objectForKey:my_number]
it returns null.
If I try and convert NSNumber to a integer via [my dict objectForKey:[my_number intValue]]
I get a warning and the program crashes when it reaches that part of the code.
Any help would be greatly appreciated.
Keys in a NSDictionary
or NSMutableDictionary
must be objects, like NSNumber
. They cannot be primitive data types, like int
.
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html
Looks like you're trying to use an integer as the key in your NSDictionary
. This would be correct with an NSArray
, with an NSDictionary
actually needs a proper object as a key.
You might have more success in this particular case feeding that data into an NSArray
, and accessing it with:
id *someData = [my_array objectAtIndex:1];
精彩评论