How can I find out if NSNumber returns from -valueForKey: was int or float originally?

The reason is that I have to return the exact value, as it was in the property. Therefore, if it was floating, I want to call -floatValue. But if it was an int, I want to call -intValue.

Is NSNumber a memorization of how it was initialized?

+6
source share
1 answer

NSNumber is a free bridge with CFNumber (see, among other sources, the text at the top of CFNumber Link ). This way you can use CFNumberGetType . For instance.

- (void)logTypeOf:(NSNumber *)number { switch(CFNumberGetType((CFNumberRef)number)) { case kCFNumberSInt8Type: NSLog(@"8bit signed integer"); break; case kCFNumberSInt16Type: NSLog(@"16bit signed integer"); break; case kCFNumberSInt32Type: NSLog(@"32bit signed integer"); break; /* ... etc, for all of: kCFNumberSInt64Type kCFNumberFloat32Type kCFNumberFloat64Type kCFNumberCharType kCFNumberShortType kCFNumberIntType kCFNumberLongType kCFNumberLongLongType kCFNumberFloatType kCFNumberDoubleType kCFNumberCFIndexType kCFNumberNSIntegerType kCFNumberCGFloatType */ } } 

EDIT: Having examined the documentation in more detail, CFNumberIsFloatType seems to do exactly what you want, without complexity. So:

 if(CFNumberIsFloatType((CFNumberRef)number)) { NSLog(@"this was a float"); } else { NSLog(@"this was an int"); } 
+16
source

Source: https://habr.com/ru/post/886567/


All Articles