ios NSDictionary 从键中检索值时出现问题
ios issue with NSDictionary retrieving value from key
我有以下几行:
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'
注意:我已经按照建议设置了符号断点,但我仍然很困惑为什么会收到此错误
那是因为字典包含一个NSNumber
。这样做:
NSNumber *value = [JSON objectForKey:@"backseatBucks"];
我有以下几行:
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'
注意:我已经按照建议设置了符号断点,但我仍然很困惑为什么会收到此错误
那是因为字典包含一个NSNumber
。这样做:
NSNumber *value = [JSON objectForKey:@"backseatBucks"];