how to extract 64 bit signed integer value
I'm querying a NSDictionary for a value. Because I got problems I printed the object. It shows me the following:
<CFString 0x5d33630 [0x26af380]>{contents = "myDictionaryItem"} = <CFNumber 0x5d58940 [0x26af380]>{value = +1286301600, type = kCFNumberSInt64Type}
So it is of type signed 64 bit integer. So I tried to extract the value like this
NSString *myString = [NSString stringWithFormat:@"%qi", [myDictionary objectForKey:@"myDictionaryItem"]];
That gives me a value but the value is not the one in the dictionary.
I also tried this
long long my64bitsignedIntegerValue = (long开发者_JS百科 long)[myDictionary objectForKey:@"myDictionaryItem"];
but I get the warning Cast from pointer to integer of different size
. That doesn't work because stringWithFormat
is returning a value of type id
. I found this post where id
is of type UInt32
. And a signed 64 bit integer is not unsigned 32 bit integer, so I get this warning. If I ignore the warning, the value is still wrong.
The value I would like to have is e.g. 1286301600. A signed integer (−2.147.483.648 to 2.147.483.647) should do the thing I want. I don't understand why Cocoa is trying to make a 64 bit signed Integer.
How can I extract the 64 bit signed integer of the dictionary and use it as a variable?
[myDictionary objectForKey:@"myDictionaryItem"]
Returns object (I suppose - NSNumber), not a scalar number. If it is really a NSNUmber object then you can get a number from it using one of the following functions:
-intValue
-longValue
-longLongValue
The following line must produce correct string from your number:
NSString *myString = [NSString stringWithFormat:@"%qi", [[myDictionary objectForKey:@"myDictionaryItem"] intValue]];
You want:
[[myDictionary objectForKey:@"myDictionaryItem"] integerValue];
or
[[myDictionary objectForKey:@"myDictionaryItem"] longLongValue];
CFNumber/NSNumber is a wrapper object around a number primitive.
I'm guessing it's an NSNumber, so something like this should work:
NSNumber *num = [myDictionary objectForKey:@"myDictionaryItem"];
long numAsLong = [num longValue];
long long numAsLongLong = [num longLongValue];
etc.
精彩评论