ios问题与NSDictionary从key检索值

时间:2015-04-23 22:39:33

标签: ios objective-c nsdictionary

我有以下几行:

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if (error) {
    NSLog(@"Error serializing %@", error);
}
NSLog(@"JSOND: %@", JSON);
NSString *valueFromKey = [JSON objectForKey:@"backseatBucks"];

NSLog(valueFromKey);
NSLog(valueFromKey.class);

第一个NSLog将打印:

JSOND: {
backseatBucks = 5;
}

然而,*valueFromKey行似乎有错误,因为未执行以下2个打印语句。错误列在下面 - 任何帮助表示赞赏:

2015-04-23 17:32:35.382 BackseatDriver2[3554:113938] JSOND: {
backseatBucks = 5;
}
2015-04-23 17:32:35.383 BackseatDriver2[3554:113938] -[__NSCFNumber length]: unrecognized selector sent to instance 0x7a64a810
2015-04-23 17:32:35.384 BackseatDriver2[3554:113938] *** Terminating app due     to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: 
unrecognized selector sent to instance 0x7a64a810'

注意:我已根据建议设置了符号断点,但仍然完全混淆了为什么我会收到此错误

1 个答案:

答案 0 :(得分:1)

那是因为字典包含NSNumber。这样做:

NSNumber *value = [JSON objectForKey:@"backseatBucks"];