I have a JSON object that is coming from a webserver.
The log is something like this:
{ "status":"success", "UserID":15, "Name":"John", "DisplayName":"John", "Surname":"Smith", "Email":"email", "Telephone":null, "FullAccount":"true" }
Note the Telephone is coming in as null if the user doesn't enter one.
When assigning this value to a NSString
, in the NSLog
it's coming out as <null>
I am assigning the string like this:
NSString *tel = [jsonDictionary valueForKey:@"Telephone"];
What is the correct way to check this <null>
value? It's preventing me from saving a NSDictionary
.
I have tried using the conditions [myString length]
and myString == nil
and myString == NULL
Additionally where is the best place in the iOS documentation to read up on this?
So a good test might be: if (title == (id)[NSNull null] || title. length == 0 ) title = @"Something"; Show activity on this post.
To find the difference between null and undefined, use the triple equality operator or Object is() method. To loosely check if the variable is null, use a double equality operator(==). The double equality operator can not tell the difference between null and undefined, so it counts as same.
JSON has two types of null valueWhen the key is provided, and the value is explicitly stated as null . When the key is not provided, and the value is implicitly null .
<null>
is how the NSNull singleton logs. So:
if (tel == (id)[NSNull null]) { // tel is null }
(The singleton exists because you can't add nil
to collection classes.)
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