gpt4 book ai didi

swift - 如果字典类型是 [String : Any], 我能得到特定值的值吗?

转载 作者:行者123 更新时间:2023-11-28 14:18:14 24 4
gpt4 key购买 nike

let josmith = [["Name": "Joe Smith"], ["Height": 42], ["SE": "YES"], ["Guardian Name(s)": "Jim and Jan Smith"]]

如何获取 Int 值 42?

最佳答案

您已经创建了一个字典数组,例如 [[String:Any]]。你说你想要一本字典,[String:Any],所以改为像这样构造它(请注意,不需要将元素放在新行上,它只是有助于提高可读性):

let josmith = ["Name": "Joe Smith",
"Height": 42,
"SE": "YES",
"Guardian Name(s)": "Jim and Jan Smith"]

然后像这样访问它:

if let fortyTwo = josmith["Height"] as? Int {
// fortyTwo == 42
} else {
// josmith did not contain "Height" or it wasn't an Int
}

关于swift - 如果字典类型是 [String : Any], 我能得到特定值的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52043612/

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