Is there a way I can check to see if a value is NULL/Nil before assigning it to a BOOL?
For example, I have a value in a NSDictionary that can be either TRUE/FALSE/NULL
mySTUser.current_user_following = [[results objectForKey:@"current_user_following"]boolValue];
When the value is NULL I get the following error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSNull boolValue]: unrecognized selector sent to instance
I would like to be able to handle the NULL case.
You can test first and assign then conditionally, e.g. something like the following:
This:
objectForKey:
twice by storing the result in a variableif
statementnil
is is equivalent to 0val
is notnil
To additionally check for the value being
NSNull
you'd have to add another test as given by ChristopheD, but i question wetherNSNull
is really needed here -YES
/NO
should be sufficient for a description like "is following".If you have no useful value for a key, you could simply remove it from the dictionary or not insert it in the first place.
You should check for
[NSNull null]
: