I have a JSON object that is coming from a webserver.
The log is something like this:
{
\"status\":\"success\",
\"Us
if([tel isEqual:[NSNull null]])
{
//do something when value is null
}
Here you can also do that by checking the length of the string i.e.
if(tel.length==0)
{
//do some logic here
}
Try this:
if (tel == (NSString *)[NSNull null] || tel.length==0)
{
// do logic here
}
I use this:
#define NULL_TO_NIL(obj) ({ __typeof__ (obj) __obj = (obj); __obj == [NSNull null] ? nil : obj; })
if we are getting null value like then we can check it with below code snippet.
if(![[dictTripData objectForKey:@"mob_no"] isKindOfClass:[NSNull class]])
strPsngrMobileNo = [dictTripData objectForKey:@"mobile_number"];
else
strPsngrMobileNo = @"";
I usually do it like this:
Assuma I have a data model for the user, and it has an NSString property called email, fetched from a JSON dict. If the email field is used inside the application, converting it to empty string prevents possible crashes:
- (id)initWithJSONDictionary:(NSDictionary *)dictionary{
//Initializer, other properties etc...
id usersmail = [[dictionary objectForKey:@"email"] copy];
_email = ( usersmail && usersmail != (id)[NSNull null] )? [usersmail copy] : [[NSString alloc]initWithString:@""];
}