Ios 访问Firebase中的用户帐户时生成完成块错误
我试图在从firebase表中提取用户配置文件时创建完成块。我需要在允许它传回值之前完成它 以下是我到目前为止的情况:Ios 访问Firebase中的用户帐户时生成完成块错误,ios,swift,firebase,firebase-authentication,completionhandler,Ios,Swift,Firebase,Firebase Authentication,Completionhandler,我试图在从firebase表中提取用户配置文件时创建完成块。我需要在允许它传回值之前完成它 以下是我到目前为止的情况: func getProf(email: String, pass: String, completionBlock: @escaping (_ success: Bool) -> (Int)) { let ref = Database.database().reference() let userID = Auth.auth().curre
func getProf(email: String, pass: String, completionBlock: @escaping (_ success: Bool) -> (Int)) {
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.zDaily = value?["qday"] as? Int ?? 0
}) {
if let error = error {
completionBlock(false)
} else {
completionBlock(true)
return zDaily
}
}
}
我得到以下错误:
Cannot convert value of type '() -> _' to expected argument type '((Error) -> Void)?'
我不知道如何解决这个问题,如果您有任何建议,我们将不胜感激。我不确定这是否能解决错误。如果没有,那么我想我知道这个问题,它将与您的错误块和您的observeEvent有关
func getProf(email: String, pass: String, completionBlock: @escaping (Bool, Int) -> ()) {
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.zDaily = value?["qday"] as? Int ?? 0
}) { (error) in //Added return error value - this may fix your error
if let error = error {
completionBlock(false, 0) //Add default 0 to return if error
} else {
completionBlock(true, zDaily) //Added zDaily as Int to return
//return zDaily - return will not return in async function so you can return the value in completionBlock above.
}
}
}
编辑:刚刚做了一个更改,从observeEvent返回一个错误对象
func getProf(email: String, pass: String, completionBlock: @escaping (Bool, Int) -> ()) {
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.zDaily = value?["qday"] as? Int ?? 0
}) { (error) in //Added return error value - this may fix your error
if let error = error {
completionBlock(false, 0) //Add default 0 to return if error
} else {
completionBlock(true, zDaily) //Added zDaily as Int to return
//return zDaily - return will not return in async function so you can return the value in completionBlock above.
}
}
}
它正在抛出条件绑定的初始值设定项必须具有可选类型,而不是“Error”错误。它不喜欢错误的可选链接。还在想办法解决这个问题。嗯,从来没有收到通知-你能解决这个错误吗?