嗨我有一個問題,並會感謝任何意見或答案。從數據庫檢索值
func getUserProfileMDP(){
// set attributes to textField
var ref: DatabaseReference!
ref = Database.database().reference()
let user = Auth.auth().currentUser
print(user!.uid)
ref.child("users").child((user?.uid)!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
guard let value = snapshot.value as? [String: String] else { return }
print(value)
let passwordValue = value["password"]!as! String
print(passwordValue)
self.MDP = passwordValue // prints the right value from database
}){ (error) in
print(error.localizedDescription)
}
print(self.MDP) // prints the value innitialised in class(nope)
}
這裏是從數據庫中獲取值的函數。它的工作原理(第一個打印得到正確的值)
@IBAction func register(_ sender: Any) {
print(self.MDP)// prints the value innitialised in class(nope)
getUserProfileMDP()
print(self.MDP) // prints the value innitialised in class(nope)
let MDP = self.MDP
那是我需要密碼來比較它。它不會讓我的數據庫的初始化類以上的價值,但價值:
var MDP = "nope"
有一個愉快的一天
因爲'.observeSingleEvent'在'getUserProfileMDP()'是異步的。所以程序控制流程不是你想象的那樣。使用回撥! – Moritz
我是新來的swift你能準確地知道什麼是「回調」? –