1

i'm trying to follow this tutorial here http://ios-blog.co.uk/iphone-development-tutorials/parsing-json-on-ios-with-asihttprequest-and-sbjson/ to do both the ASIHttp and SBJSon tutorial at the same time and all is well until the line

NSMutableArray *colorTitles = [jsonDictionary objectForKey:@"title"];

where it would crash with the error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x9340d20'

i've tried using NSArray, NSString and am running out of ideas... my codes are exavtly the same with the link

acvon
  • 161
  • 3
  • 6
  • 21

3 Answers3

1

This Probably happens when you try Call the method which don't belong corresponding Class or it superClass then in this case you get error:unrecognized selector sent to instance.

So Check what JSON you getting. According to the JSON Response(data)try to parse it.

I'd like to tell you about the JSON:

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

In JSON, they take on these forms:

Object

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

Array:

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

Value

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

I think you are getting the Value JSON, so Try below. and one thing you can not assign(create object) the value to NSmutableArray Literally. so Please do in below Manner.

NSArray *colorTitles = [jsonDictionary valueForKey:@"title"];

If you still don't get the Proper result.

http://www.json.org/

Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
  • uh sorry the format ran off, but its something like that, and yea i changed it to nsarray but still the same result with the error – acvon Mar 04 '12 at 06:18
  • sorry pasted an irrelevant json but the json is as http://www.colourlovers.com/api/colors?format=json as im doing the tutorial – acvon Mar 04 '12 at 06:22
  • and i think the json structure is of an array of objects? – acvon Mar 04 '12 at 06:24
  • here you get the Array of values and your values are objects, please let me check more.wait for a while...!! – Kamar Shad Mar 04 '12 at 06:25
  • Use NAArray instead of NSMUtableArray As: NSArray *colorTitles = [jsonDictionary valueForKey:@"title"]; – Kamar Shad Mar 04 '12 at 06:27
  • if ([jsonDictionary isMemberOfClass:[NSArray class]]) { NSLog(@"if here"); NSArray *t = [jsonDictionary objectForKey:@"title ]; }else { NSLog(@"else here"); NSDictionary *ids = [jsonDictionary objectForKey:@"title"]; } – acvon Mar 04 '12 at 06:35
  • @eddy use this i think you getting the the JSON response as Values NSArray *colorTitles =[jsonDictionary valueForKey:@"title"];. – Kamar Shad Mar 04 '12 at 06:40
  • kamar! ure a lifesaver! thanx that did the trick, its valueForKey LOL thanx! – acvon Mar 04 '12 at 06:56
0

objectForKey is a method of NSDictionary, not NSArray. You are probably missing (or overdoing) a step in the JSON string parsing.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • i've followed exactly the steps on that website, could it be the type that i'm allocating the objectForKey is wrong? if yes, what should be the right type? – acvon Mar 04 '12 at 05:21
  • @eddy please see you are assinging the some Value to NSMutableArray litrally , which is not right ,in this point crash mayoccur.and second thing you need to check your JSON response(what you get values, array, object).in your code you calling [jsonDictionary objectForKey:@"title"];.it may be possible you are getting values instead of Object. SO check your Response – Kamar Shad Mar 04 '12 at 06:11
0

Usually, this kind of error comes when you are trying to access a dealloc'd object. Check if you are over-releasing or not retaining the jsonDictionary.

Sailesh
  • 25,517
  • 4
  • 34
  • 47
  • am i right to do by retaining the `jsonDictionary, it would mean i need to declare it in the .h file with the @property(retain) like that? or am i getting the wrong idea :( – acvon Mar 04 '12 at 05:20
  • No need to go to .h file. Only check that you are not releasing the object before this line. If you have declared the variable in the same method, this should work. Could you paste your code for this method? – Sailesh Mar 04 '12 at 05:25
  • @Sailesh this is not true this eror comes when your are trying access the method which is not exist in the Corresponding class(or it's subclass). – Kamar Shad Mar 04 '12 at 05:42
  • Yes, but why is that happening in this case, when the object is declared as NSMutableDictionary? This is because the object is dealloc'd, and at run time, that memory address represent some random data, which is falsely read as a `__NSArrayM` object. Note that this is not even `NSArray` but some internal class. Hence, one plausible explanation is that whatever resided on that mem location is now released and not accessible. – Sailesh Mar 04 '12 at 05:50
  • I only declared the NSMutable/NSArray in the method and nope its not released as far as i can see... – acvon Mar 04 '12 at 06:19
  • the method is `-(void) requestFinished: (ASIHTTPRequest *) request { NSString *theJSON = [request responseString]; SBJsonParser *parser = [[SBJsonParser alloc] init]; // Actually parsing the JSON NSMutableDictionary *jsonDictionary = [parser objectWithString:theJSON error:nil]; NSLog(@"the json %@", jsonDictionary); NSString *key = @"itemname"; NSArray *t = [jsonDictionary objectForKey:key ]; }` – acvon Mar 04 '12 at 06:20
  • `[parser objectWithString:.. ]` must be returning an array and not a dictionary. Put a check like: `if ([parsedObject isMemberOfClass:[NSDictionary class]]) { //do stuff } – Sailesh Mar 04 '12 at 06:25