Storing long long values in Objective-C
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: -7331108254开发者_运维问答952843887 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?
There is no encodeLongLongValue:forKey:
No, but there is an encodeInt64:forKey: which should do the trick.
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.
If the speed is not your critical concern, just treat it as a string and use NSString*
.
精彩评论