Obj-C defining integer values ​​with isKindOfClass

I need to check the type of each item in an array ...

for(id obj in items) {
    if([obj isKindOfClass:[NSString class]]) {
       //handle string case
    } else if([obj isKindOfClass:[NSInteger class]]) {  //THIS LINE GIVES ERROR
       //handle int case
    }
}

      

Of course, NSInteger

is this just an alias for int

, so how can I check this at runtime?

+2


source to share


1 answer


You are not actually storing NSInteger

in NSArray

, as it is not an object. If you are storing numbers in your array, they are most likely instances NSNumber

, so you should check:

if ([obj isKindOfClass:[NSNumber class]]) { ... }

      



IPhone Developer Tips gives a good summary of the difference between NSInteger

and NSNumber

.

+3


source







All Articles