原因是我必须回到房产中的确切价值。所以如果它是一个浮点数,我想调用-floatValue。但如果它是一个int,我想调用-intValue。
NSNumber是否记得它是如何初始化的?
答案 0 :(得分:16)
NSNumber与CFNumber免费联网(请参阅CFNumber reference顶部的文字,除其他来源外)。所以你可以使用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
*/
}
}
编辑:更详细地查看文档,CFNumberIsFloatType似乎可以完全按照您的要求执行,而不会产生复杂性。所以:
if(CFNumberIsFloatType((CFNumberRef)number))
{
NSLog(@"this was a float");
}
else
{
NSLog(@"this was an int");
}