gpt4 book ai didi

ios - 在 Swift 中获取 Firebase 子数据

转载 作者:可可西里 更新时间:2023-11-01 01:09:27 25 4
gpt4 key购买 nike

我已经在 Xcode 9 上设置了 Firebase。

我的问题:我想从 Firebase 检索数据。这行得通,但是当我在组中添加另一个文本时,它不会检索。

示例

火力基地:

lang :

"Deutsch"

"Englisch"

Firebase 图像示例,因为它很难解释:

Firebase Image Example beacause it's difficult to explain

import UIKit
import FirebaseDatabase

class ViewController: UIViewController {

@IBOutlet weak var outputone: UILabel!
// @IBOutlet weak var outputtwo: UILabel!

var dbReference: DatabaseReference?
var dbHandle: DatabaseHandle?

override func viewDidLoad() {
super.viewDidLoad()

dbReference = Database.database().reference()
// dbReference?.child("lang").childByAutoId().setValue("Deutsch")
// dbReference?.child("lang").childByAutoId().setValue("Englisch")

// retrieve data
dbHandle = dbReference?.child("lang").observe(.childAdded, with:{ (snapshot) in
let lang:String? = snapshot.value as? String
self.outputone.text = (lang)
})

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

我希望在 outputone 中显示“Deutsch”,在 outputtwo 中显示“Englisch”

有人可以帮忙吗?

最佳答案

现在您检索整个 lang 节点。所以 snapshot你从 Firebase 得到的包含 "de": "Deutch""en": "English"。当您现在执行 snapshot.value 时? String,它返回 nil,因为快照没有单个简单的字符串值。

要获取具体值,您首先需要告诉快照您想要哪个子属性:deen然后您可以得到字符串值。

dbHandle = dbReference?.child("lang").observe(.value,  with:{(snapshot) in
let de:String? = snapshot.childSnapshot(forPath:"de").value as? String
let en:String? = snapshot.childSnapshot(forPath:"en").value as? String
self.outputone.text = (de)
self.outputtwo.text = (en)
})

关于ios - 在 Swift 中获取 Firebase 子数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48584103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com