Search code examples
iosnsstringsbjson

My code is throwing Exception Unrecognized selector sent


This is the code I am using to get string coming through a SBJson parsing:

NSMutableArray *abc=[jsonData valueForKey:@"items"];

NSString *imgStr=(NSString *)[abc valueForKey:@"image"];
NSLog(@"%@",imgStr);

here abc is NSMutableArray

and the exception it is throwing is

> -[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance

Solution

  • In the first line you declare abc to be an NSMutableArray

    In the second line you attempt to access it as an NSDictionary.

    In your case (and I am guessing here), I expect you have an array of items, and each item is a dictionary with an "image" key.

    So to get your first item you might use

      NSDictionary* firstItem = [abc objectAtIndex:0];
    

    Then you can extract your image from that:

    NSString *imgStr=(NSString *)[firstItem valueForKey:@"image"];1

     NSString *imgStr = [abc objectForKey:@"image"];
    

    1 see comment from @Stig