Search code examples
iphoneobjective-cios4

NSInteger != nil


How can I check that the NSInteger is valid?

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

Solution

  • 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.