无法检索数据并在UIBarButton中使用它-Firebase Swift 3

马克斯·科特

我正在尝试检索用户的用户名并将其保存到变量,尽管每次运行代码时都不会发生。这是我的Firebase布局:在此处输入图片说明

这是我的代码 viewDidAppear()

let uid = FIRAuth.auth()?.currentUser?.uid
let ref:FIRDatabaseReference!
ref = FIRDatabase.database().reference().root.child("users").child("\(uid)")
ref.observeSingleEvent(of: .value, with: { snapshot in
    print("test")
    if !snapshot.exists() { return }

    let user = (snapshot.value as? NSDictionary)?["name"] as? String ?? ""
    print(user)
    let username = UIBarButtonItem(title: user, style: UIBarButtonItemStyle.plain, target: self, action: nil)
    let picture = UIBarButtonItem()
    self.navigationController!.navigationItem.rightBarButtonItem = username
})
德拉威

尝试使用:-

FIRDatabase.database().reference().root.child("users").child("\(FIRAuth.auth()!.currentUser!.uid)")observeSingleEvent(of: .value, with: { snapshot in
   print("test")
   if let snapDict = snapshot.value as? [String:AnyObject]{
          let user = snapDict["name"] as! String
          print(user)
          let username = UIBarButtonItem(title: user, style: UIBarButtonItemStyle.plain, target: self, action: nil)
          let picture = UIBarButtonItem()
          self.navigationItem.rightBarButtonItem = username
        } else {
           print("No such user exists!.")
      }
 })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章