2

I am working through Apple's sample code for displaying a Twitter feed from here http://bit.ly/x4nhG5

From the example, the code is taking the JSON and putting it into an NSDictionary:

        NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError]; 

What I was just trying to do at this point was look at all the keys that came back by doing this:

NSArray *allKeysArray = [publicTimeline allKeys];

I then receive an error while trying to run the program:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray allKeys]: unrecognized selector sent to instance 0x6a950f0'

Any reason why this NSDictionary that is loaded from a JSON is behaving this way?

Thanks, Flea

2 Answers 2

13

If you look at the exception you see '-[__NSCFArray allKeys], this indicates that you actually have an __NSCFArray which is a private subclass of NSArray. This is not an NSDictionary, which is why you get the exception.


If you look at the JSON feed it is of the form

[                             
    {
        coordinates: null,
        truncated: false,
        // ...
    },
    {
        coordinates: null,
        truncated: false,
        // ...
    }
]                            

In JSON [] represents an array and {} represents an object.

An [JSON] array is an ordered collection of values

This means a JSON array can easily be mapped to an NSArray.

An [JSON] object is an unordered set of name/value pairs

This means a JSON Object can be mapped to an NSDictionary.


So looking at the feed we can see that we actually have an array of objects. Which NSJSONSerialization will turn into an NSArray of NSDictionary's. Therefore to get to a dictionary we first need to access it from the array first resulting in:

NSDictionary *tweet = [publicTimeline objectAtIndex:0];

NSArray *allKeys    = [tweet allKeys];
2
  • 2
    Thanks Paul. Apple in their code was delcaring *publicTimeLine as a NSDictionary. In order do use your suggestion; I had to declare *publicTimeLine as an NSArray. By doing it this way: NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError]; I was able to use your examples. Thanks!
    – Flea
    Jan 16, 2012 at 20:31
  • 1
    NSDIctionary may not respond to objectAtIndex problem occurs. Why?. Am using iOS 6. Apr 21, 2014 at 14:04
1

What the error tells you is that your publicTimeline is not NSDictionary but NSArray.

So, my guess is that instead of

NSArray *allKeysArray = [publicTimeline allKeys];

this would work as you intend.

NSArray *allKeysArray = [[publicTimeline objectAtIndex:0] allKeys];
1
  • 1
    Barley, the NSDictionary does not contain a method of objectAtIndex. Would you have another suggestion?
    – Flea
    Jan 16, 2012 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.