Tal Zamirly
Tal Zamirly

Reputation: 117

Unrecognized selector when parsing JSON

So I have the photograph below which is a screenshot I took of a JSON script I am trying to read from a URL. In the code section below, I managed to get the 'name' attribute and assign it to the 'name' NSString object, but when I try do the same for 'address' I get the following error:

[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980'

whats the difference between how I did it inside 'venues' than inside 'location'? I tried many different ways to do it, some including arrays, but nothing seems to work.

enter image description here

NSURL *url = [NSURL URLWithString:urlString];

NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

NSDictionary *responseDictionary = [dataDictionary objectForKey:@"response"];
NSDictionary *venueDictionary = [responseDictionary objectForKey:@"venues"];

for (NSDictionary *dict in venueDictionary) {
    NSLog(@"%@", dict);
    NSString *name = [dict objectForKey:@"name"];
    NSDictionary *locationDictionary = [dict objectForKey:@"location"];
    for (NSDictionary *locationDict in locationDictionary) {
        NSString *address = [locationDict objectForKey:@"address"];
        NSLog(@"%@", address);
    }
}

Upvotes: 0

Views: 149

Answers (1)

David Berry
David Berry

Reputation: 41226

You're expecting the "location" key to point to an array, but it doesn't. It points to an object, get rid of for (NSDictionary *locationDict in locationDictionary) { and just pull the address out of locationDictionary instead.

Upvotes: 3

Related Questions