Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSInteger != nil

How can I check that the NSInteger is valid?

NSInteger previousScore = [[self score] integerValue];
    if (previousScore != nil) {
        //do something
    }
like image 766
dreadbot Avatar asked Jan 25 '11 01:01

dreadbot


2 Answers

If you want to check for validity in your code, you'd do something like:

NSNumber *previousScore = [self score];

if ( previousScore != nil ) {
  NSInteger previousScoreValue = [previousScore integerValue];
  // do something
}

This works as you are getting back an object, not a primitive value.

like image 96
Kendall Helmstetter Gelner Avatar answered Nov 07 '22 01:11

Kendall Helmstetter Gelner


NSInteger isn't an object. It's simply a typecasted primitive int. Therefore, it will never be nil. Just treat it the same as if you were using an int straight up.

Edit: To expound upon Cesar's comment, on 64-bit systems NSInteger is actually a long and on 32-bit systems it's an int.

like image 13
Marc W Avatar answered Nov 07 '22 00:11

Marc W