【发布时间】:2017-01-01 00:28:15
【问题描述】:
我正在尝试设置一个循环以从 json 字典中检索信息,但该字典位于保护语句中:
guard let resultsDictionary = jsonDictionary["result"] as? [[String : Any]]?,
let costDictionary = resultsDictionary?[0],
let cost = costDictionary["cost"] as? [String: Any],
let airbnb = cost["airbnb_median"] as? [String: Any]{
for air in airbnb {
let airbnbUS = air["USD"] as Int
let airbnbLocal = air["CHF"] as Int
}
else {
print("Error: Could not retrieve dictionary")
return;
}
当我这样做时,会出现多个错误:
在 'guard' 条件之后预期的 'else', 在“守卫”条件下声明的变量在其主体中不可用, 大括号语句块是未使用的闭包
我不知道为什么它不起作用
【问题讨论】:
-
你的
guard逻辑很不靠谱 - 语法是guard statements else { /* error handling */ } /* the regular logic using airbnb */ -
虽然这个问题可能会因为基于意见而被关闭,但如果有一种标准化的方式来表达这一点,那就太好了。对我来说,@i_am_jorf 应该是第二种方式。