ios - swift 将个人的名字从 firebase 读入 UILabel

标签 ios swift firebase firebase-realtime-database uilabel

我成功地读取了个人的名字,并且可以在控制台中打印这个值,但是我怎样才能成功地将它传递到 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/

相关文章:

ios - Firebase 数据库分离监听器

ios - 如何为所有按钮定义单个 UILongPressGestureRecognizer?在 swift 3

swift - alamofire 对象映射器 .. 为什么打印我的项目名称?

ios - Swift - 可达性崩溃

ios - 如何在 Xcode 中更改 UITableView 边框颜色

swift - 如何同时控制两个UITextField的数据?

java - 仅当密码正确时,Firebase 才会更改 "DisplayName"

python - (Flask API),使firebase上传的图像具有唯一的名称

ios - 如何仅转义未转义的引号 Objective C

ios - 打开应用程序商店作为模型 View 打开空白页面 ios