开发者

NSDictionary with integer as number

开发者 https://www.devze.com 2023-01-28 17:48 出处:网络
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

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消