ios firebase数据库获取值的关键

时间:2018-06-14 00:16:24

标签: ios swift firebase firebase-realtime-database

抱歉新手在这里。我似乎找不到任何可以帮助我从firebase实时数据库中获取以下信息的解决方案(见图)

orange rectangle marks structure of data and data to be retrieve

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

这是我目前的代码

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")
}) 

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

1 个答案:

答案 0 :(得分:1)

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

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)
        }
    }
})