The reason is I must get back the exact value as it was in the property. So if it was a float, I want to call -floatValue. But if it was an int, I want to call -intValue.
Is NSNumber remembering how it was initialized?
NSNumber is toll-free bridged with CFNumber (see, amongst other sources, the text at the top of the CFNumber reference). So you can use CFNumberGetType. E.g.
- (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: looking more thoroughly at the documentation, CFNumberIsFloatType would appear to do exactly what you want without the complexity. So:
if(CFNumberIsFloatType((CFNumberRef)number))
{
NSLog(@"this was a float");
}
else
{
NSLog(@"this was an int");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With