简体   繁体   中英

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.

Here's the code I use when interfacing with the 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? Why is it not liking it?

Oddly enough, if I inspect it at runtime it says it's an 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM