Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obj-C determing integer values with isKindOfClass

Tags:

objective-c

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?

like image 896
Ben Scheirman Avatar asked Sep 12 '26 03:09

Ben Scheirman


1 Answers

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.

like image 179
e.James Avatar answered Sep 14 '26 23:09

e.James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!