I need to check the type of each element 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 just an alias for int, so how can I check for this at runtime?
You can't actually store NSInteger in an NSArray, since it isn't an object. If you are storing numbers in your array, they are most likely instances of NSNumber, so you would check for:
if ([obj isKindOfClass:[NSNumber class]]) { ... }
iPhone Developer Tips gives a good summary of the difference between NSInteger and NSNumber.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With