在方法中设置全局变量

时间:2018-06-18 15:04:52

标签: ios swift

//Global variable:
var senderString = ""

 override func viewDidLoad() {
...
set()
print(senderString)
}

func set() {
    if Auth.auth().currentUser?.uid == nil{
        logout()
    } else{
        let uid = Auth.auth().currentUser?.uid
        let ref = Database.database().reference()
        ref.child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
            if let dic = snapshot.value as? [String: AnyObject] {
                self.senderString = dic["username"] as! String
                print("this"+self.senderString)
            }
            })
    }

}

如何将全局变量senderString设置为dic [" username"] as!字符串?我希望senderString全局变量反映用户名。

0 个答案:

没有答案