我使用[NSNumber numberWithInt:42]
或@(42)
将int转换为NSNumber,然后再将其添加到NSDictionary中:
int intValue = 42;
NSNumber *numberValue = [NSNumber numberWithInt:intValue];
NSDictionary *dict = @{ @"integer" : numberValue };
当我从NSDictionary检索值时,如何将其从NSNumber转换回int?
NSNumber *number = dict[@"integer"];
int *intNumber = // ...?
它抛出一个异常,表示我以这种方式进行铸造时需要铸造:
int number = (int)dict[@"integer"];