check if value is integer or double in objective c
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.
精彩评论