dreadbot
dreadbot

Reputation: 962

NSInteger != nil

How can I check that the NSInteger is valid?

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

Upvotes: 15

Views: 15371

Answers (2)

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.

Upvotes: 16

Marc W
Marc W

Reputation: 19251

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.

Upvotes: 13

Related Questions