0

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
Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
Atul
  • 111
  • 3
  • 12
  • Show your JSON please. And add an `NSLog(@"%@", abc);` line and show the output of this before the crash. I suspect `abc` is not what you think it is. – Stig Brautaset Jan 24 '13 at 09:56
  • this is the NSLog for abc in my consol `2013-01-24 16:14:06.535 Drase[1381:11303] ( { gamepoint = 20; gid = 365; image = "iVBORw0KGgksjallajlasldjlasdjlasjljlajsdlajldal"; level = 1; movie = ""; status = pending; uid = 13; } )` the image is short as it is not coming in the comment – Atul Jan 24 '13 at 10:46

2 Answers2

4

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

foundry
  • 31,615
  • 9
  • 90
  • 125
  • oh! i have no idea about that can you tell me how to access NSMutableArray @HeWas – Atul Jan 24 '13 at 09:50
  • @Atul, see my expanded answer – foundry Jan 24 '13 at 10:03
  • 1
    Please, don't advice using `valueForKey:`. It's the wrong method. Learn to use `objectForKey:` which is the method the OP wants. – Stig Brautaset Jan 24 '13 at 11:38
  • @StigBrautaset - corrected, thanks. I always would use objectForKey - but I was trying to leave the rest of the code as-is so that Atul could focus on the issue that was giving him the error. – foundry Jan 24 '13 at 14:33
  • 1
    @HeWas Excellent. The reason I say that is that the OP probably wouldn't have been confused in the first place if he'd used the correct method, as the error would have been much more obvious. – Stig Brautaset Jan 24 '13 at 15:50
0

Your code has several problems:

  1. For NSArray, valueForKey: returns another array. It does not return an NSString, regardless of your cast. You need get a reference to the first entry in the array, then call objectForKey: on that.

  2. You're using the NSKeyValueCoding method valueForKey: where you're probably intending to use the NSDictionary method objectForKey:. For dictionaries they are roughly interchangeable, but if you'd used objectForKey: you would have got an easy to understand error about not being able to call objectForKey: on an array. Use valueForKey: only when you really want key-value-coding semantics.

  3. You almost never need to cast id in Objective-C. You're only hiding errors that way. (It may or may not be hiding your problem in this instance, but it's certainly uglier, incorrect (because you're trying to cast an array to a string) and just fooling you into thinking you have a string because the cast didn't fail.)

You probably meant to write something like this:

NSArray *abc = [jsonData valueForKey:@"items"];
NSDictionary *entry = [abc objectAtIndex:0];    
NSString *imgStr = [entry objectForKey:@"image"];
NSLog(@"%@", imgStr);
Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39