Reputation: 1121
I'm trying to parse a some very basic json data, i doing the exact same whey i always do but i getting the following error:
-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8e7d570 2014-04-30 15:04:33.699 Mensagens 2[7530:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8e7d570'
this is the GET.JSON file:
{
"version":"6"
}
This is the code in my app where i try to get value of Version:
NSURL *url = [NSURL URLWithString:@"http://localhost/app/get.json"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSMutableArray *getVersion = [[NSMutableArray alloc]init];
getVersion = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSString *currentVersion = [[getVersion objectAtIndex:0]objectForKey:@"version"];
NSLog(@"Version = %@", currentVersion);
I just don't see any where things are going wrong.
Upvotes: 0
Views: 1405
Reputation: 343
Irrespective of the type you used to declare getVersion, the below method creates NSDictionary or NSArray depending upon the data passed to this method. In your case, it seems that JSON response data is a dictionary, hence even though getVersion is declared (or for that matter even instantiated) to be NSMutableArray object, after execution of below getVersion is a NSDictionary.
getVersion = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
As answered by meda, you can use [getVersion objectForKey:@"Version"] to get what you're looking for.
Upvotes: 0
Reputation: 45500
It should be
NSDictionary *getVersion = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions error:nil];
NSString *currentVersion = [getVersion objectForKey:@"version"];
or with new syntax
NSString *currentVersion = getVersion[@"version"];
because objectAtIndex
means you are accessing an array but you only have a json object
Upvotes: 1