【发布时间】:2015-09-28 15:26:31
【问题描述】:
我正在从 NSMutableDictionary 中获取一个元素,该元素是通过从 NSURLSessionDataTask 取回数据并使用 NSJSONSerialization 将其反序列化而创建的,如下所示:
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
所以在返回 JSON 后,我将它缓存在 NSCache 中,然后当我再次拿起它使用时,我尝试获取一个似乎显示为 NSNumber 的元素:
NSString *hopefullyAString = [[NSNumber numberWithLong:settings[@"hopefullyAString"] stringValue]];
现在我设置断点,这就是我插入控制台的内容:
(lldb) po labs(hopefullyAString.longValue)
error: property 'longValue' not found on object of type 'NSString *'
error: 1 errors parsing expression
(lldb) po [hopefullyAString isKindOfClass:[NSString class]]
false
这很奇怪,因为一方面我无法获得 NSString 的 longValue,但显然,所谓的字符串不是 NSString。怎么回事?
最终,这就是我想要的:
anotherString = [anotherString stringByAppendingString:hopefullyAString];
但是当我插入它时,我得到一个错误:
(lldb) po queryParams = [anotherString stringByAppendingString:hopefullyAString]
2015-09-28 11:22:09.946 SomeApp[826:265400] -[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000004cfa73
error: Execution was interrupted, reason: internal ObjC exception breakpoint(-3)..
The process has been returned to the state before expression evaluation.
如果我尝试通过断点播放(附加字符串):
2015-09-28 11:23:41.804 SomeApp[826:265400] -[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000004cfa73
2015-09-28 11:23:41.806 SomeApp[826:267251] XPC connection interrupted
2015-09-28 11:23:41.809 SomeApp[826:265400] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000004cfa73'
*** First throw call stack:
(0x182154f5c 0x196c57f80 0x18215bc6c 0x182158c14 0x18205cdcc 0x182fac88c 0x1000db8d4 0x1000db670 0x1000d390c 0x1879ed67c 0x1879ed7d4 0x1879dd3d4 0x1879f2364 0x187793a90 0x1876a700c 0x186eadf14 0x186ea8b20 0x186ea89e0 0x186ea807c 0x186ea7dd0 0x186ea14bc 0x18210bc30 0x1821099d4 0x182109e04 0x182038dc0 0x18d18c088 0x187712f60 0x1000d5f6c 0x1974828b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
我知道这可能是我不理解的小问题,所以提前谢谢。
编辑 1:忘了提及...我正在使用 Xcode 7,不确定这是否相关,但几周前当我回到 Xcode 6 时,相同的代码仍在工作。
【问题讨论】:
-
是设置字典吗?
-
尝试:
id hopefullyAString = settings[@"hopefullyAString"]; NSLog("Type %@", [hopefullyAString class]);它应该记录该键的值的类型,这将帮助您弄清楚发生了什么。 -
是的,设置是一本字典 - 谢谢,我试过了......现在它正在工作。希望这是一个编码修复,但我想如果您对 Xcode 有出色的更新,请尽快接受。
标签: ios objective-c json nsstring