Passing NSInteger variable to NSMutableDictionary or NSMutableArray

后端 未结 7 891
北恋
北恋 2020-12-28 12:42

Why does this not work:

NSInteger temp = 20;
[userSettingsFromFile setObject:temp forKey:@\"aTemp\"];

but this does:

[userS         


        
相关标签:
7条回答
  • 2020-12-28 13:03

    NSInteger isn't an object -- it's simply typecast to int on 32-bit or long on 64-bit. Since NSDictionary can only store objects, you need to wrap the integer into an object before you can store it. Try this:

    NSInteger temp = 20;
    [userSettingsFromFile setObject:[NSNumber numberWithInteger:temp] 
                             forKey:@"aTemp"]; 
    
    0 讨论(0)
提交回复
热议问题