IOS_5555 IOS_5555 - 3 months ago 8x
iOS Question

How to display string value in label when the value is null and some number

I have JSON object like this.

"id": "1",
"subTotal": "20000.00",
"total": "124565.00",
"timeOfTrans": "3555525",
"status": "1",
"name": "gjgjgf",
"email": "",
"level": "gjfgj",
"teamId": "1"
"id": "2",
"subTotal": null,
"total": null,
"timeOfTrans": null,
"status": null,
"name": "Rajendra",
"email": "",
"level": "gfjgfjg",
"teamId": "1"

I want to display JSON string "total" in label. When "total" is null i want to display '0' when "total" is some number i want to display that number in same label.

Here i`m trying this code

totalLbl=(UILabel *)[cell viewWithTag:1];
id total = [currentEmplyCellDit objectForKey:@"total"];
if ([total isKindOfClass:(id)[NSNull null]])
//Your value of total is NULL
totalLbl.text = @"0";
//Show the Value.
totalLbl.text = [NSString stringWithFormat:@"%@",total];

this code displaying correctly but i shows null in label when "total" is null.

I want to display 0 when "total" is NULL

How to Solve this...


You can make condition to check null in your string and then replace this with whatever you want.

id yourValue=[youJSONDict objectForKey:@"total"];;
if ([yourValue isKindOfClass:[NSNull class]]) {
    yourLabel.text=[NSString stringWithFormat:@"%@",yourValue];