简体   繁体   English

为什么我会收到“ [__NSArrayI allKeys]:无法识别的选择器发送到实例” /为什么要进行NSDictionary转换?

[英]Why am I getting “[__NSArrayI allKeys]: unrecognized selector sent to instance” / why is NSDictionary transforming?

Looking this [__NSArrayI allKeys]: unrecognized selector sent to instance error up, it seemingly occurs when you send an NSArray the allKeys message which is meant for NSDictionary , but in this case I'm very clearly sending it to an NSDictionary. 查看此[__NSArrayI allKeys]: unrecognized selector sent to instance错误,似乎是在向NSArray发送用于NSDictionaryallKeys消息时发生的,但在这种情况下,我非常清楚地将其发送到NSDictionary。

Here's the code I use when interfacing with the Pocket API: 这是与Pocket API接口时使用的代码:

        NSDictionary *articles = [response objectForKey:@"list"];

        // Create an array we can use to sort the keys (and thus the articles) in order of when they were added
        NSMutableArray *allKeys = [[articles allKeys] mutableCopy];

The last line there causes the error. 那里的最后一行导致错误。 But articles is very clearly declared as an NSDictionary? 但是articles非常明确地声明为NSDictionary吗? Why is it not liking it? 为什么不喜欢它?

Oddly enough, if I inspect it at runtime it says it's an NSArray! 奇怪的是,如果我在运行时检查它,则说它是一个NSArray! Why did it change? 为什么会改变?

(lldb) po articles
$5 = 0x082103e0 <__NSArrayI 0x82103e0>(

)

(lldb) po [articles class]
$6 = 0x01b83b8c __NSArrayI
(lldb) 

It may be declared in your code as a dictionary, but that doesn't make it a dictionary. 它可以在您的代码中声明为字典,但这并不能使其成为字典。 It is truly an array and that is why you get the exception. 它确实是一个数组,这就是为什么您会得到异常。 Check your response so you know what you should expect. 检查您的response以便您知道应该做什么。

Your code compiles because the compiler doesn't know that it's going to be an array and it trusts you that it will be a dictionary. 您的代码可以编译,因为编译器不知道它将是一个数组,并且相信您它将是一个字典。 It does this because objectForKey: returns id . 这样做是因为objectForKey:返回id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到一个“无法识别的选择器发送到实例”错误? - Why am I getting a `unrecognized selector sent to instance` error? 为什么我的MapViewController收到无法识别的选择器发送到实例异常? (iOS) - Why am I getting a unrecognized selector sent to instance exception with my MapViewController? (iOS) 为什么我会收到发送到 class 的无法识别的选择器? - Why am I getting unrecognized selector sent to class? 为什么我收到CIFilter的“无法识别的选择器发送给类”错误? - Why am I getting an “unrecognized selector sent to class” error for CIFilter? [__NSArrayI pickerView:numberOfRowsInComponent:]:无法识别的选择器已发送到实例 - [__NSArrayI pickerView:numberOfRowsInComponent:]: unrecognized selector sent to instance -[__ NSArrayI addObject:]:无法识别的选择器已发送到实例 - -[__NSArrayI addObject:]: unrecognized selector sent to instance [__NSArrayI length]:无法识别的选择器已发送到实例 - [__NSArrayI length]: unrecognized selector sent to instance [__NSArrayI removeObjectAtIndex:]: 无法识别的选择器发送到实例 - [__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 为什么无法识别的选择器被发送到实例? - Why Unrecognized selector sent to instance? 无法识别的选择器发送到实例 - 为什么? - Unrecognized selector sent to instance - why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM