Firebase-在Swift中将所有子键和子值检索到单独的数组中 [英] Firebase - retrieve all child keys and child values into separate arrays in Swift

查看:69
本文介绍了Firebase-在Swift中将所有子键和子值检索到单独的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检索子级中的所有键,将它们放入数组中,然后检索这些键的值并将其放入另一个数组中?

Is there any way to retrieve all the keys in a child, put them into array, and then retrieve the values for the keys and put it into another array?

源代码:

self.ref?.child("data").child("success").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
   if snapshot != nil {
        // Your answer goes here
   }
}

推荐答案

快照具有两个属性.

snapshot.key
snapshot.value

当使用带有.value的观察者时.快照中返回所有键:value子代.可以对它们进行迭代以捕获每个键:像这样的值对.

When using an observer with .value. all of the key: value children are returned in the snapshot. They can be iterated over to capture each key: value pair like this.

ref.observeSingleEvent(of: .value, with: { (snapshot) in
    for child in snapshot.children {
        let snap = child as! DataSnapshot
        let key = snap.key
        let value = snap.value
        print("key = \(key)  value = \(value!)")
    }
})

请记住,value属性可以是字符串,数字,数组或其他字典(快照).在最初的问题中,它是一个字符串.

Keep in mind that the value property could be a string, number, array or another dictionary (snapshot). In the original question it's a String.

这篇关于Firebase-在Swift中将所有子键和子值检索到单独的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆