Gezim Kryeziu
Gezim Kryeziu

Reputation: 29

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]

and 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

Upvotes: 0

Views: 2381

Answers (2)

TheJeff
TheJeff

Reputation: 4101

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

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

Upvotes: 0

bbum
bbum

Reputation: 162712

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.

Upvotes: 1

Related Questions