gpt4 book ai didi

ios - 嵌套字典中的 Swift 访问字段

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:54 25 4
gpt4 key购买 nike

我必须处理像这样或更多嵌套的字典。我怎样才能访问像“twotwo”这样的字段?或者是否有更好的可能性来模拟这种结构?

let nestedDict = [
"fieldOne": "name",
"fieldTwo": "name",
"fieldThree":
[
[
"twoOne": "some text",
"twoTwo": true,
"twoThree": 1e-40
],
[
"twoOne": "some text",
"twoTwo": true,
"twoThree": 1e-40
]
]
]

最佳答案

nestedDict 是一个字典,你得到fieldThree

let fieldThree = nestedDict["fieldThree"] as! [[String:Any]] // [[String:AnyObject]] in Swift 2 and lower.

fieldThree[String:AnyObject] 字典的Array,你得到 twoTwo 的值带有

的第一个数组项
let twoTwo = fieldThree[0]["twoTwo"] as! Bool

您甚至可以检索数组中键 twoTwo 的所有值

let allTwoTwo = fieldThree.map { $0["twoTwo"] as! Bool }

关于ios - 嵌套字典中的 Swift 访问字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33259519/

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