ios - 如何读取 Firebase 子值?

标签 ios swift firebase swift3 firebase-realtime-database

enter image description here

我是编码和 Swift 的新手。 我正在尝试从我的应用程序中的 Firebase 数据库中检索 house1Colour 的值。到目前为止,我已经尝试过这些方法。

let eg = FIRDatabase.database().reference(withPath: "test")

(当我使用它时,我得到一个 THREAD 1 Signal SIGABRT 错误,我不确定为什么)

和:

var test:String!

FIRDatabase.database().reference().child("house1Colour").observeSingleEvent(of: .value, with: {(snap) in
    if let snapDict = snap.value as? Dictionary <String, AnyObject>{
        self.test = snapDict["house1Colour"] as! String

        print(self.test)
    }
})

它们都不起作用。

最佳答案

FIRDatabase.database().reference().child("house1Colour") 的值只是字符串,因为您已经指定了 key house1Colour .

因此你应该能够:

if let snapString = snap.value as? String {
    print(snapString)
}

关于ios - 如何读取 Firebase 子值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41475199/

相关文章:

ios - 强调字体 swift 的粗体

android - 如何使用 Firebase 仅使用电话号码和 otp 实现现有用户登录

node.js - 使用环境变量对 Firebase Admin SDK 进行身份验证

ios - 在 iOS 中没有 userControl 的情况下在 Webview 中播放 Youtube Url

ios - 单元格中的个人资料图片未正确更新

ios - DeepLink 支持 iOS 8 和 iOS 9

ios - 包裹的 CATextLayer 中的字符被部分截断

json - 类型 'JSON' 的值没有成员 'arrayOf'

swift - 在 UIviewcontroller 的按钮操作中使用 viewDidLoad 中的变量

android - 谁拥有这封电子邮件? ... @cloudteSTLabaccounts.com