【问题标题】:How to read the data type Dictionary to type [String]?如何读取数据类型Dictionary来键入[String]?
【发布时间】:2019-10-13 03:18:23
【问题描述】:

这是我的数据结构:

问题是我不知道如何正确恢复也是 Key 的数据。我想要的只是键和值“true”只是标记。而且我什至不知道我当前存储的是什么类型的数据以及如何将键值恢复。

我想要这个:

["BAFS", "Econ", "Geog"]

以下是我写数据的方式:

var subjectvalue: [String] = []
let structure = subjectvalue.reduce(into:[String:String](), { $0[$1] = "true"})
 let postRef = Database.database().reference().child("posts").child(id!).child("posts").setValue(structure)

我已经囤了几天了。有人有想法可以帮忙吗?提前谢谢你。

【问题讨论】:

标签: swift firebase firebase-realtime-database


【解决方案1】:

您可以从DataSnapshot 获取字典。然后获取字典的.keys,去掉所有不是"true"的元素

let postRef = Database.database().reference().child("posts").child("id").child("posts")
postRef.observe(.value) { snapshot in
    if let dict = snapshot.value as? [String : String] {
        let result = Array(dict.keys).removeAll(where: { $0 != "true" })
        print(result)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    相关资源
    最近更新 更多