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

标签 ios swift dictionary nested

我必须处理像这样或更多嵌套的字典。 我怎样才能访问像“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/

相关文章:

android - 如何在 react 导航中获取当前路线名称?

objective-c - Swift 中的 _cmd 供选择器使用

swift - 在不影响老用户的情况下更新代码流

python - 访问元组字典中所有元素的相同值

ios - 从 iOS 中的离屏 OpenGL 像素缓冲区读取像素 (OopenGL-ES)

ios - 当 ViewController 重新出现时保留其值的正确方法

ios - 如何在 textView 的高度增加时动态更改 textView 的 super View 高度

ios - 如何在每 N 个单词后将子字符串作为分隔符添加到段落

java - 将一个值放入 Java 中的映射中*而不*更新现有值(如果存在)

dictionary - 为什么在 Clojure 的 transient 映射中插入 1000 000 个值会生成一个包含 8 个项目的映射?