gpt4 book ai didi

swift - 从字典 Swift 中检索数据

转载 作者:可可西里 更新时间:2023-11-01 01:36:28 24 4
gpt4 key购买 nike

我正在尝试从字典中检索数据并将值分配给“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
}

最佳答案

你的本地人应该这样定义:

let locals: [[String: Dictionary<String, String>]] = [["Jerry":["City": "Seattle", "Preference": "Adventurer"]],["Emily":["City": "Boston", "Preference": "History Buff"]]]

然后就可以这样访问了:

let jerry = locals[0]["Jerry"]

关于swift - 从字典 Swift 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36962835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com