ios - Swift firebase 获取数据

标签 ios swift firebase firebase-realtime-database

如何从这个结构中获取name, quant:

enter image description here

我试试这个:

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
    }

当我打印 snapshot.value 时,它从 Quote1 给我数据,但我想在 textField 中显示名称并在标签中显示 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
  }
}

并且有错误。如何将数据从 snapshot.value 添加到标签和文本字段?

最佳答案

当我检查你的屏幕时,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
         }
     }
}

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

相关文章:

android - 即使全部被覆盖,Firebase 也会显示自动通知

iOS 8 : Autorotation is not working without storyboard

ios - TKChartDataPoint 不符合预期的类型序列

iphone - UITabBar 模态视图 Controller

ios - 在 Swift 中通过多点连接临时同步两个 iOS 设备

swift - 替换 Storyboard 中的 ViewController,同时保留 Segues

android - ProgressBar 是否应该在达到 100% 后等待片刻?

ios - 将 linphone 集成到 iOS

ios - UITableViewCell 内部的 UICollectionView

java - 无法使用 Intent 和 Parcelable 传输对象