Why is my NSNumber always zero?
So, for some reason, in a certain method, mine is NSNumber
always nil
and driving me nuts.
If the user selects a row in my picker, it gets called and works fine:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.query.activityLevel = [NSNumber numberWithInt:[[[self.activityArray objectAtIndex:row]objectForKey:@"key"]intValue]];
}
However, when the user doesn't select a line and just goes to the next VC, I have a check to see if there is activityLevel
nil
and set it to 1
if it is.
- (void)donePressed {
if (!self.query.activityLevel) {
self.query.activityLevel = [NSNumber numberWithInt:2];
}
NSNumber *calories = [Calculations numberOfCaloriesNeededForQuery:self.query];
[self dismissModalViewControllerAnimated:YES];
}
The problem is that whenever the user doesn't select a row and hits it self.query.activityLevel = [NSNumber numberWithInt:2];
, if I print the object, it doesn't care nil
. I have no idea why. I set a breakpoint and ended up on this line ...
source to share
You might be getting null because the value is self.query
also null, so you set the value on the object to nil and get null ...
It's a little confusing, but while in another language you usually get a NullPointer exception in situations like this, in a C object it is legal to send a message zero, so pay attention to these situations ...
source to share