[英]Swift firebase get data
How can I get name, quant
from this structure:我怎样才能从这个结构中得到name, quant
:
Im try this:我试试这个:
var handle: DatabaseHandle?
var ref: DatabaseReference?
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
handle = ref?.child("Цитаты").child("Из Книг").child("Дорогой звезд").child("Quote1").observe(.childAdded, with: { (snapshot) in
let user = snapshot.value
print(user!) //text from name + 10 + Quote1
}
When I print snapshot.value
it's give me data from Quote1
, but I want to show name in textField and quant in label I try this:当我打印snapshot.value
它会从Quote1
给我数据,但我想在 textField 中显示 name 并在 label 中显示 quant 我试试这个:
ref = Database.database().reference()
handle = ref?.child("Цитаты").child("Из Книг").child("Дорогой звезд").child("Quote1").observe(.childAdded, with: { (snapshot) in
let user = snapshot.value
if let us = user!["name"] as? String {
if let quant = user!["quant"] as? String{
self.quoteTextField.text = us
self.quotelabel.text = quant
}
}
And have error.并有错误。 How can I add data from snapshot.value to label and textField?如何将 snapshot.value 中的数据添加到 label 和 textField?
As I was checking your screen, quant is an Int not String, so try this当我检查你的屏幕时,quant 是一个 Int 而不是 String,所以试试这个
ref = Database.database().reference()
handle = ref?.child("Цитаты").child("Из Книг").child("Дорогой звезд").child("Quote1").observe(.childAdded, with: { (snapshot) in
//let username = snapshot.value as? NSDictionary
let user = snapshot.value as? [String: Any]
if let us = user!["name"] as? String {
if let quant = user!["quant"] as? Int {
self.quoteTextField.text = us
self.quotelabel.text = quant.description
}
}
}
You need to cast the snapshot.value
as a dictionary -您需要将snapshot.value
为字典 -
let user = snapshot.value as? NSDictionary
You should also consider safely unwrapping your optionals -您还应该考虑安全地打开您的选项 -
let name = user?["name"] as? String ?? ""
let quant = user?["quant"] as? Int ?? 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.