如何在标签中加载Firebase中的文本?

时间:2018-05-03 14:00:58

标签: ios swift xcode firebase label

我需要从孩子那里加载文字"尼克" 我的程序每次都在void上更改标签文本:(

这是我的代码:

  ref = Database.database().reference()
refHandle = ref.observe(DataEventType.value, with: { (snapshot) in
    let dataDict = snapshot.value as! [String: AnyObject]
    print(dataDict)
})

let userID: String = (Auth.auth().currentUser?.uid)!
ref.child("Users").child(userID).child("Nick").observeSingleEvent(of: .value, with: { 
    snapshot in 
    print(snapshot)
    let m = snapshot.value as? String
    self.label.text = m
})

And this is how my Firebase looks like

1 个答案:

答案 0 :(得分:0)

您必须从快照值中取值“Nick”。所以你的代码看起来像这样:

let userID: String = (Auth.auth().currentUser?.uid)!
ref.child("Users").child(userID).child("Nick").observeSingleEvent(of: .value, with: { 
    snapshot in 

    guard let value = snapshot.value as? [String: Any] else { return }
    guard let name = value["Nick"] as? String else { return }

    let m = name
    self.label.text = m
})