gpt4 book ai didi

objective-c - 我如何确定是否从 -valueForKey : was initially an int or float? 返回的 NSNumber

转载 作者:太空狗 更新时间:2023-10-30 03:40:09 25 4
gpt4 key购买 nike

原因是我必须取回属性中的确切值。所以如果它是一个 float ,我想调用 -floatValue。但如果它是一个 int,我想调用 -intValue。

NSNumber 是否记得它是如何初始化的?

最佳答案

NSNumber 是与 CFNumber 桥接的免费电话(除其他来源外,请参阅 CFNumber reference 顶部的文本)。所以你可以使用 CFNumberGetType .例如

- (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");
}

关于objective-c - 我如何确定是否从 -valueForKey : was initially an int or float? 返回的 NSNumber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5770887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com