//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全局变量反映用户名。