iOS Firebase数据库获取价值的关键 [英] ios firebase database get key of value

查看:36
本文介绍了iOS Firebase数据库获取价值的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,新手在这里.我似乎找不到任何能帮助我从Firebase实时数据库中获得以下信息的解决方案(见图片)

Sorry newbie here. I can't seem to find any solution that helps me get the following information from the firebase realtime database (see image)

橙色矩形标记数据的结构和要检索的数据

这是我当前的代码

ref.child("locations").observe(.value, with: { snapshot in
        for child in snapshot.children{
            let valueD = child as! DataSnapshot
            let keyD = valueD.key
            let value1 = valueD.value
            print(value1)
            // This gives "-L-other letters" = 0 (but I only want the string without "= 0")
}) 

有什么办法可以做到这一点?谢谢!

Is there any way I can do this? Thanks!

推荐答案

如果 locations 是屏幕快照中显示的内容的根,那么您只会遍历第一级子级( 37d42 ... 等).要获得标记的键,您需要更深一层地循环.所以:

If locations is the root of what you show in the screenshot, you're only looping over the first level of children (37d42... etc). To get the keys you marked, you need to loop one level deeper. So:

ref.child("locations").observe(.value, with: { snapshot in
    for child in snapshot.children{
        for grandchild in child.children{
            let valueD = grandchild as! DataSnapshot
            let keyD = valueD.key
            let value1 = valueD.value
            print(keyD)
        }
    }
}) 

这篇关于iOS Firebase数据库获取价值的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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