我目前正在开发一个应用程序,它返回以下格式的json:
"location_subtype" = "somevalue"; "location_type" = Force; month = "2015-01"; "outcome_status" = { category = "somevalue"; date = "somevalue"; };
如果“outcome_status”有值,则显示类别和日期,但是如果“outcome_value”没有任何值(这在大多数情况下都是如此),则显示如下:
"location_subtype" = "somevalue"; "location_type" = Force; month = "2015-01"; "outcome_status" = ""; "persistent_id" = "";
问题是如何检查outcome_status的值是否不为“null”?
我尝试了以下方法将类别和日期存储到标签中,但会出现错误。第一个if语句应该检查值是否不为null,如果不是则转到下一个if语句。但是它继续执行下一个if语句,并且我收到以下错误:
Thread 1: EXC_BAD_ACCESS(code=2, address=0x102089600)
if (dict["outcome_status"] != nil)
{
if ((dict["outcome_status"]as NSDictionary)["category"] != nil)
{
outcomeStatusLabel.text = ((dict["outcome_status"]as NSDictionary)["category"] as NSString)
outcomeStatusLabel.font = UIFont.systemFontOfSize(14.0);
outcomeStatusLabel.numberOfLines = 0
}
if ((dict["outcome_status"]as NSDictionary)["date"] != nil)
{
outcomeDateLabel.text = ((dict["outcome_status"]as NSDictionary)["date"] as NSString)
outcomeDateLabel.font = UIFont.systemFontOfSize(14.0);
outcomeDateLabel.numberOfLines = 0
}
}
如果我删除第一个if语句,只有当 "outcome_status" = "null" 时它才会崩溃,如果 "outcome_status" 中有一些值,它就可以完美地运行。
我需要做什么才能使它在值为null时停在第一个if语句处呢?
提前感谢您。
if (dict["outcome_status"] as NSString != "")
它仍然会崩溃。 - chirag90