-1

I'm writing an iOS application that gets data from a web request. The request returns the following: {"hash":"0369a5d5e65335309b2b1502dc96b5aba691b9451c83b9","error":0}

I get the data from the NSData* responseData object as follows:

NSDictionary* JSONdata = [NSJSONSerialization JSONObjectWithData:_responseData options:0 error:&error];

NSInteger responseError = (NSInteger)[JSONdata objectForKey:@"error"];

However, responseError is coming back uninitialized (filled with garbage values). I tried changing NSInteger to NSString* but that yields the following error

'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x9d02220'

Any ideas?

Idris
  • 997
  • 2
  • 10
  • 27
jtmarmon
  • 5,727
  • 7
  • 28
  • 45

1 Answers1

2

You need to use an NSNumber. Primitive values will not work for as iOS parses the data in NSObjects: NSArray, NSDictionary, NSNumber, NSString, and NSNull.

Try this: NSNumber *responseError = [JSONdata objectForKey:@"error"];

Then to convert to an NSInteger you can do this:
NSInteger responseInt = [responseError integerValue]; this will return an integer value.

Also for future reference, an NSDictionary cannot contain any primitive values.

Milo
  • 5,041
  • 7
  • 33
  • 59