swift - 如何使用 Swift 4 从 firebase 中的 child 那里获取特定值?

标签 swift firebase firebase-realtime-database key children

我的 Firebase 数据库

enter image description here

更具体地说,我随机生成了子项(列表),我想从这些随机生成的列表中获取键的字符串值。

例如,如果我想要图片 URL 地址,我想获取键“PhotoURL:”的字符串值。

提前致谢!

最佳答案

首先您需要做的是import Firebase 然后从数据库类中调用一个函数,如下所示:

let ref = Database.database().reference().child("Listings")

你可以递归地调用 child 来深入你的树

//.child("Listings").child("SomeListing").child("PhotoURL")

然后调用 observeSingleEvent 从 firebase 接收值。 您的值存储在快照变量中

ref.observeSingleEvent(of: .value, with: { (snapshot) in    
            guard let listingsDictionary = snapshot.value as? [String: Any] else { return }

            listngsDictionary.forEach({ (key, value) in
                       // Here you can iterate through it
            })
        }) { (err) in
            print("Failed to fetch following listings:", err)
        }

关于swift - 如何使用 Swift 4 从 firebase 中的 child 那里获取特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53514553/

相关文章:

javascript - Firebase 开关按钮

swift - 清空 Collection View Swift

swift - 字符串成员 'characters' 返回什么?

ios - 从操作内部访问队列

ios - Firebase - iPhone X 上的 iOS 应用安装不起作用

javascript - 如何查找Firestore( Node )中是否存在记录

swift - Firebase 2,访问快照中的子值会产生空值

ios - 如何在 ios/swift 中将 url 转换为字符串?

ios - UITextField 渐变背景颜色

angular - 路由超时。 Angular 9 通用 + Firebase