我尝试从 nsdictionary 获取所有值,但它在第二行抛出异常
NSDictionary* thetimeLineDict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
NSArray* theallTweets = [thetimeLineDict allValues];
下面是控制台的异常
2011-11-27 14:56:38.156 SparkTweet[2066:1390b] -[__NSCFArray allValues]: unrecognized selector sent to instance 0x8128310
2011-11-27 14:56:38.158 SparkTweet[2066:1390b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray allValues]: unrecognized selector sent to instance 0x8128310'
*** First throw call stack:
(0x14c0052 0x189cd0a 0x14c1ced 0x1426f00 0x1426ce2 0x2c48 0x33306 0x1b38445 0x1b39ecf 0x1b39d28 0x1b394af 0x9b632b24 0x9b6346fe)
terminate called throwing an exception
为什么会这样?
最佳答案
因为 allValues
不是返回的 NSArray 的有效方法。
JSON 解析器并不总是返回字典作为最外层的 Objective-C 对象。相反,您返回的内容取决于输入的 JSON 文本,而输入的 JSON 文本有一个数组 ([]
) 作为最外层结构。
(请注意,作为一般规则,您应该始终测试 JSON 解析器的结果以查看返回的类型,除非您绝对确定它始终是数组或“对象”/字典。)
关于objective-c - 为什么在 NSDictionary 上调用 -allValues 会引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8283585/