ios - 无法检索数据并在 UIBarButton 中使用它 - Firebase Swift 3
<p><p>我正在尝试检索用户的用户名并将其保存到变量中,尽管每当我运行代码时都没有任何反应。这是我的 Firebase 布局:
<a href="/image/QZhIk.png" rel="noreferrer noopener nofollow"><img src="/image/QZhIk.png" alt="enter image description here"/></a> </p>
<p>这是我的代码,在我的 <code>viewDidAppear()</code></p>
<pre><code>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
})
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试使用:- </p>
<pre><code>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? {
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!.")
}
})
</code></pre></p>
<p style="font-size: 20px;">关于ios - 无法检索数据并在 UIBarButton 中使用它 - Firebase Swift 3,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/40054043/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/40054043/
</a>
</p>
页:
[1]