[英]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发送用于NSDictionary
的allKeys
消息时发生的,但在这种情况下,我非常清楚地将其发送到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.