-1

I have the following few lines:

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if (error) {
    NSLog(@"Error serializing %@", error);
}
NSLog(@"JSOND: %@", JSON);
NSString *valueFromKey = [JSON objectForKey:@"backseatBucks"];

NSLog(valueFromKey);
NSLog(valueFromKey.class);

The first NSLog will print:

JSOND: {
backseatBucks = 5;
}

However it seems to be erroring on the *valueFromKey line because the following 2 print statements are not executed. The error is listed below- any help is appreciated:

2015-04-23 17:32:35.382 BackseatDriver2[3554:113938] JSOND: {
backseatBucks = 5;
}
2015-04-23 17:32:35.383 BackseatDriver2[3554:113938] -[__NSCFNumber length]: unrecognized selector sent to instance 0x7a64a810
2015-04-23 17:32:35.384 BackseatDriver2[3554:113938] *** Terminating app due     to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: 
unrecognized selector sent to instance 0x7a64a810'

Note: I have set symbolic breakpoints as suggested and am still thoroughly confused why I would be getting this error

GregH
  • 5,125
  • 8
  • 55
  • 109
  • 1
    possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Apr 23 '15 at 22:43
  • 4
    `NSLog` should never be used as you have it. It should always first have a string format parameter first, then any needed arguments. `NSLog(@"%@", valueFromKey);`. – rmaddy Apr 23 '15 at 22:43

1 Answers1

1

That's because the dictionary contains an NSNumber. Do this:

NSNumber *value = [JSON objectForKey:@"backseatBucks"];
nhgrif
  • 61,578
  • 25
  • 134
  • 173
Dreaming In Binary
  • 1,197
  • 6
  • 8
  • That won't make his code work any better (though it will cause it to raise a compile error, I suspect). – Hot Licks Apr 23 '15 at 22:44
  • This made no change- still receiving the same error. and I believe JSON always returns strings – GregH Apr 23 '15 at 22:49
  • 1
    @jeremy This is half of the fix. The other half is to fix your `NSLog` statements like I mention in my earlier comment. And no, JSON does not always return strings. Read the error. It is an `NSNumber`, not an `NSString`. – rmaddy Apr 23 '15 at 23:23
  • I have changed valueFromKey to an NSNumber and removed the second and third NSLog statement. Still receiving the same error – GregH Apr 23 '15 at 23:52
  • 1
    @jeremy - Well, quit trying to use an NSNumber as if it were an NSString!! – Hot Licks Apr 23 '15 at 23:58