我成功地读取了个人的名字,并且可以在控制台中打印这个值,但是我怎样才能成功地将它传递到 UILabel 中呢?
当我尝试如下所示传递它时,我收到错误“'(viewController) -> () -> viewController' 类型的值没有成员 'FirstName'”
var ref: 数据库引用!
var FirstName: String?
func observeName() { // this is the function that will return the name of the individual to ensure that it is displayed on the HomeViewController
guard let uid = Auth.auth().currentUser?.uid else { return }
let ref = Database.database().reference().child("users").child("profile").child(uid)
ref.queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let firstName = value?["First Name"] as? String
self.FirstName = firstName
}) { (error) in
print(error.localizedDescription)
}
}
let welcomeText: UILabel = {
let l = UILabel()
l.text = "Hi...\(self.FirstName)"// WHERE I WANT THE NAME!!
l.font = UIFont(name: "Optima-ExtraBlack", size: 30)
l.textColor = .white
return l
}()
我希望这会返回刚刚登录的用户的名称。任何帮助都会很棒!一直在为此转动我的轮子。
最佳答案
在 firebase 函数的完成处理程序中设置 welcomeText.text = firstName
。
关于ios - swift 将个人的名字从 firebase 读入 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57103587/