我怎样才能只从Firebase获取密钥? [英] How can i get only keys from firebase?

查看:71
本文介绍了我怎样才能只从Firebase获取密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有图像上的数据库结构,我需要在红色矩形中显示此日期.我试图这样做,但会引发错误,并且我在堆栈中找不到相同的问题.

I have a structure of database as on image and I need to display this date which is in the red rectangle. I tried to do smth like this, but it throws an error and I couldn't find same questions on a stack.

我的数据库

reference.child("doc1").observe(.value, with: { (snapshot) in
            if snapshot.exists() {
                for date in (snapshot.value?.allKeys)
            }

推荐答案

您的结构是字典词典,因此您必须将快照转换为 [String:[String:Any]] 关键是您的"11dot ...",并且值包含所有小时数

Your structure is a Dictionary of Dictionary so you have to cast your snap to [String:[String:Any]] where the key is your "11dot..." and value contains all hours

因此,请尝试使用以下代码:

So try to use this code:

guard let dict = snap.value as? [String:[String:Any]] else { return }
for (key, value) in dict {
    for (key2, value2) in value {
        print(key2, value2) // this print your hours
    }
}

无论如何,我建议您不要使用observe(.value)来读取所有子节点上发生的所有更改.而是使用观察者的 .childAdded 功能.

Anyway I suggest you to don't use a observe(.value) which will read all change happened on all child node. Instead use the .childAdded feature of observer.

使用 .childAdded 时,您一次只会收到一个孩子(例如子节点上的for),之后只会添加一个孩子:

With a .childAdded you will receive only one child at a time (like a for on child node) and after that only the child added:

Database.database().reference().child("doc1").observe(.childAdded) { (snap) in
    guard let dict = snap.value as? [String:Any]
    print(dict) // this print data contains on "11dot10" and so on  
}

这篇关于我怎样才能只从Firebase获取密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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