我正在尝试创建完成块,同时从firebase表中拉取用户配置文件。在我允许它传回值之前,我需要它完成。
这是我到目前为止所知道的:
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)?'我不确定如何解决这个问题,任何建议都将不胜感激。
发布于 2020-09-14 11:36:40
我不确定这是否能修复这个错误。如果没有,那么我想我知道问题出在哪里了,那就是你的错误块和你的observeEvent。
编辑:刚做了一个更改,从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.
}
}
}https://stackoverflow.com/questions/63877720
复制相似问题