views:

47

answers:

3

I'm working with an API that returns a JSON dictionary. One of the keys is "Variant Hash", and the corresponding data is an integer like this: -7331108254952843887 or 6209894088655053576. I'm assuming this is a long long value. I'm serializing this data with NSCoder, and I'm not sure which encodeValue: method to use. There is no encodeLongLongValue:forKey:.

Also, doing NSLog(@"%@",returnedDictionary); displays the entire dictionary structure, but using objectForKey: doesn't work because the corresponding value for the key Variant Hash is not an object.

How can I get the data out of the dictionary, and how should I store this with NSCoder?

Thank you for your help.

A: 

If the speed is not your critical concern, just treat it as a string and use NSString*.

Yuji
+1  A: 

Depending on the JSON parser you're using, the number is being stored in the dictionary as an NSNumber, NSDecimalNumber, NSString, or NSValue. NSNumber (or NSDecimalNumber, which is a subclass) is probably the most likely, but I'd check your parser's documentation or code.

Assuming that's the case, you'd get the value as such.

NSNumber *numberValue = [myDictionary objectForKey:myKey];
long long longValue = [numberValue longLongValue];

I'd probably implement some type checking in this code, just to be safe.

NSNumber conforms to NSCoding, so you can use the numberValue instance to also do your encoding.

Robot K
After coding the value to disk, and then reloading it, the number changes. It goes from the correct 19 digit number to a 10 digit integer that is not even in the same ballpark. Some of the numbers are signed, others are unsigned, but they're always 19 digits long. What could be causing this? It's driving me crazy
JustinXXVII
What if you use NSDecimalNumber instead? It's designed for bigger numbers. You can also try Jeremy's suggestion below.
Robot K
Thank you, the NSDecimalNumber did the trick!
JustinXXVII
+1  A: 

There is no encodeLongLongValue:forKey:

No, but there is an encodeInt64:forKey: which should do the trick.

JeremyP