无法从 JSON 对象中获取 NSString

Can't get an NSString out of a JSON object

我正在从 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.

调试器中的第一条错误消息是因为你告诉它 hopefullyAString 是一个字符串并且它相信你说的是真的然后试图调用一个不存在的方法NSString.

第二个调试器命令是错误的,因为现在它实际上正在检查自己是否是 NSString 并发现它不是。

您的其他错误是因为您将 NSNumber(您的 hopefullyAString 实际上是什么)传递给采用 NSString.

的方法

您分配 hopefullyAString 的代码块语法无效,无法编译,因此很难说出您具体需要更改什么,但您不应该通过numberWithLong 的 objc 对象。

error: property 'longValue' not found on object of type 'NSString *'

这不足为奇:NSString 没有 longValue 属性。但是,有一个 longLongValue 属性,我希望这就是您所想的。

-[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000004cfa73

在这种情况下,问题出在 anotherString,它似乎没有指向 NSString 的有效实例。您是否正确初始化 anotherString?你应该这样做:

NSString *anotherString = @"Some string."
anotherString = [anotherString stringByAppendingString:hopefullyAString];

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000004cfa73'

是的,终止应用程序通常是在抛出异常时发生的。修复你的代码,异常就会消失。但是,该消息告诉您您正在尝试将 -length 消息发送到 NSNumber 的实例,该实例没有 -length 方法。我猜 hopefullyAString 中的对象在 JSON 文件中表示为数字而不是字符串,因此反序列化时会得到一个数字。