0

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);
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Tal Zamirly
  • 117
  • 1
  • 11
  • Before assigning you just do Null check – Albin Joseph Feb 11 '15 at 04:24
  • 1
    @AlbinJoseph its not null, because if I try to print 'locationDict' in the for loop I get all the location categories (address, lat, long, distance etc). – Tal Zamirly Feb 11 '15 at 04:30
  • Which line of code causes the error? None of the code you posted makes an array reference. – rmaddy Feb 11 '15 at 04:31
  • 1
    Also note that `venueDictionary` should be an `NSArray`, not an `NSDictionary`. – rmaddy Feb 11 '15 at 04:31
  • And why didn't you simply update your [previous question](http://stackoverflow.com/questions/28445149/objective-c-read-json-from-url-into-nsdictionary) instead of posting a new one? – rmaddy Feb 11 '15 at 04:33

1 Answers1

3

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.

David Berry
  • 40,941
  • 12
  • 84
  • 95