How can I check that the NSInteger is valid?
NSInteger previousScore = [[self score] integerValue];
if (previousScore != nil) {
//do something
}
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.
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
.
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