开发者

check if value is integer or double in objective c

开发者 https://www.devze.com 2022-12-20 14:40 出处:网络
I have a case where I should get the elements from an array and I do not know if the type is double or integer.

I have a case where I should get the elements from an array and I do not know if the type is double or integer.

[array objectAtIndex:2]

an开发者_StackOverflowd the problem is that I can not identify the type. If i knew the type I simply would perform: [[item objectAtIndex:2] intValue] or [[item objectAtIndex:2] doubleValue]

Is there any way to detect that?

Br, Gzim


See the documentation for the -objCType method.

Basically, it returns a string describing the type contained in the NSNumber. The Objective-C runtime documentation has more information on the type codes.

However, it is quite odd to have a design where the type is unknown. I would suggest that a better solution -- possibly an interesting question -- would be to avoid the ambiguous type in the design of your application.


NSString* type = [NSString stringWithUTF8String:[valueInQuestion objCType]];

This will equal "d" if it is a double and "i" if it is an integer.

0

精彩评论

暂无评论...
验证码 换一张
取 消