gpt4 book ai didi

iphone - IOS 如何判断一个数是 NSInteger 还是 float ?

转载 作者:行者123 更新时间:2023-11-28 18:00:22 24 4
gpt4 key购买 nike

我需要检查来自服务器的值是 NSString 、 NSInteger 还是 float。如果是字符串,我可以检查。但是我们如何区分 float 和整数?

最佳答案

这是怎么做的

NSNumber * n = //from somewhere
if (strcmp([n objCType], @encode(float)) == 0) {
NSLog(@"this is a float");
} else if (strcmp([n objCType], @encode(int)) == 0) {
NSLog(@"this is an int");
}

编辑

好的,让我们让它更通用

    NSNumber *anyNSObject = [NSNumber numberWithFloat:0.4];

id obj = anyNSObject; //you can pass anything here as long as it is inherited from NSObject

if ([obj isKindOfClass:[NSString class]]) {
NSLog(@"This is string");
}

if ([obj isKindOfClass:[NSNumber class]]) {
NSLog(@"This is NSNumber");

if (strcmp([obj objCType], @encode(float)) == 0) {
NSLog(@"this is a float");
} else if (strcmp([obj objCType], @encode(int)) == 0) {
NSLog(@"this is an int");
}
}

关于iphone - IOS 如何判断一个数是 NSInteger 还是 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15020989/

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