0

We have the following method where we are trying to access an array object at a given index. The array is resultArr. When we do a resultArr count it gives us a result of 13. So we know that the array is not null but when we try to do objectAtIndex it crashes with the error.

Function:

- (void)fetchedData:(NSData *)responseData {
      //parse out the json data
   NSError* error;
   NSDictionary* json = [NSJSONSerialization 
                         JSONObjectWithData:responseData //1

                         options:kNilOptions 
                         error:&error];

   NSArray *keys = [json allKeys];
   NSLog(@"keys: %@",keys);



   NSArray* htmlAttributions = [json objectForKey:@"html_attributions"]; //2


   NSArray* resultArr = (NSArray *)[json objectForKey:@"result"]; //2

   NSArray* statusArr = [json objectForKey:@"status"]; //2


   NSLog(@"htmlAttributions: %@",htmlAttributions);


   NSLog(@"result: %@", resultArr); //3

   NSLog(@"status: %@", statusArr); //3

   NSLog(@"resultCount: %d",[resultArr count]);

   [resultArr objectAtIndex:0];
  }

Error:

2012-04-01 22:31:52.757 jsonParsing[5020:f803] resultCount: 13 2012-04-01 22:31:52.759 jsonParsing[5020:f803] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6d2f900 2012-04-01 22:31:52.760 jsonParsing[5020:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6d2f900'
*** First throw call stack:

Thank you.

2
  • This is resolved now. Thanks guys. Commented Apr 2, 2012 at 3:58
  • You should mark the question as resolved by clicking the check mark by the correct answer. Commented Apr 2, 2012 at 4:17

3 Answers 3

2

The error message is fairly descriptive. One of the objects that your code expects to be an NSArray is actually an NSDictionary. You cannot access fields inside of an NSDictionary by using NSArray methods (and casting from NSDictionary* to NSArray* will not convert an NSDictionary into an NSArray).

This would mean that inside of the JSON, one of your elements was serialized as an object/associative array instead of as a plain array. You can easily determine which one by looking at your JSON data as text, and finding the item that uses { and } instead of [ and ].

Sign up to request clarification or add additional context in comments.

Comments

0

You are saying

NSArray* resultArr = (NSArray *)[json objectForKey:@"result"]; //2

But that does not make this object ([json objectForKey:@"result"]) an NSArray. It is an NSDictionary, and sending it a message that NSDictionary does not respond to (objectAtIndex:) causes a crash.

You were able to send it the count message without crashing because NSDictionary does happen to respond to the count message. But your preconception that this is an array is still mistaken.

Comments

0

You cannot cast an NSDictionary* to an NSArray* as you tried to do with this line: NSArray* resultArr = (NSArray *)[json objectForKey:@"result"];, then call -objectAtIndex.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.