【发布时间】:2015-03-25 19:09:47
【问题描述】:
我目前正在开发一个带回 json 的应用程序,格式如下
"location_subtype" = "somevalue"; "location_type" = 强制; 月=“2015-01”; “结果状态” = { 类别=“一些值”; 日期=“某个值”; };
如果“outcome_status”有值,则显示类别和日期,但是如果“outcome_value”没有任何值(在大多数情况下),则显示如下
"location_subtype" = "somevalue"; "location_type" = 强制; 月=“2015-01”; “结果状态”=“”; "persistent_id" = "";
问题是如何检查结果状态的值是否不是“空”?
我尝试了以下方法将类别和日期存储到标签中,但它遇到错误,第一个 if 语句应该检查值是否不为空,如果它不是转到下一个 if 语句。但是它继续下一个 if 语句,我收到以下错误
线程 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 语句处停止?
提前谢谢你。
【问题讨论】:
-
@MartinR 如果我尝试以下
if (dict["outcome_status"] as NSString != "")它仍然会崩溃。