Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary having null value

I have a problem when Extracting data from a Dictionary object. Count of the Dictionary is displaying as 1, but the one value it is displaying is null. I want to display a AlertView when there is no data in the Dictionary object. I thought of displaying the AlertView when the count is '0', but it returning '1'.

I am extracting this dictionary object from a WebService using JSON.

{
     "My Data" = null;
}

Used this code for getting the "My Data" value into the Dictionary variable Datas.

Datas = (NSDictionary *) [details objectForKey:@"My Data"];

if ([Datas count] == 0) {

//code for showing AlertView....

}

Please help me to display a UIAlertView when the Dictionary value having null....

like image 931
Kalyan Urimi Avatar asked Mar 04 '13 10:03

Kalyan Urimi


2 Answers

NSDictionary and other collections cannot contain nil values. When NSDictionary must store a null, a special value [NSNull null] is stored.

Compare the value at @"My Data" to [NSNull null] to determine if the corresponding value is null or not.

// Since [NSNull null] is a singleton, you can use == instead of isEqual
if ([details objectForKey:@"My Data"] == [NSNull null]) {
    // Display the alert
}
like image 67
Sergey Kalinichenko Avatar answered Sep 19 '22 20:09

Sergey Kalinichenko


Usually null values in JSON get parsed to NSNull. So this condition should check for that as well as nil: if((details[@"My Data"] == nil) || (details[@"My Data"] == [NSNull null]))

like image 38
Carl Veazey Avatar answered Sep 22 '22 20:09

Carl Veazey