I'm facing a wierd problem with NSNumber
,When i run the following code,
NSNumber *num = [NSNumber numberWithLong:1000];
const char* type =[num objCType];
if (strcmp (type, @encode (long)) == 0) {
NSLog(@"Type is long");
}else if(strcmp (type, @encode (int)) == 0){
NSLog(@"Type is int");
}
The output is :
Type is int
Edit: I tried [NSNumber numberWithLong:1000L]
and [NSNumber numberWithLong:1000000000]
But still the same output.
Why is the type int
here??
The answer is simple... the documentation for objCType says:
Special Considerations
The returned type does not necessarily match the method the receiver was created with.
This is like asking a stringWithInt
method to return a string that still knows it is an int. You create an NSNumber
, you get an NSNumber
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.