我正在尝试从字典中检索数据并将值分配给“city”。我想使用information[1]["City"]
,但这也会产生错误。另外,不确定我是否需要在这里打开可选项。
let locals = ["Jerry":["City": "Seattle", "Preference": "Adventurer"],"Emily":["City": "Boston", "Preference": "History Buff"]
func matchTravellerWithLocal(location: String, type: String) -> String {
var message = " "
let locals = LocalsInformation().locals
for (name, information) in locals{
let city = information["City"]!
print(city)
let preference = information["Preference"]
if city == location{
message = "Meet your local: \(name), a local of \(city)"
}else{
message = "Apologies, there aren't in \(city) on LocalRetreat. Try again soon!"
break
}
if preference == type{
message += "who is also a \(preference)"
}
}
return message
}
答案 0 :(得分:2)
您的本地人应该这样定义:
let locals: [[String: Dictionary<String, String>]] = [["Jerry":["City": "Seattle", "Preference": "Adventurer"]],["Emily":["City": "Boston", "Preference": "History Buff"]]]
然后你可以这样访问它:
let jerry = locals[0]["Jerry"]