有人可以告诉我为什么我不断收到错误& quot;无效函数中的非预期非无效返回值".
问题描述:
当尝试返回true或false Bool值时出现错误...有人可以帮忙吗?我正在尝试检查用户是否存在于Firebase数据库中.
I get an error when trying to return a true or false Bool value... Can someone help ? I was trying to check if user exists in Firebase database.
func checkIfUserExists(email: String) -> Bool{
FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]{
switch email {
case dictionary["email"] as! String!:
return true
break
default:
return false
break
}
}
}, withCancel: nil)
}
答
以这种方式更改
func checkIfUserExists(email: String, results:(exists:Bool)->Void){
FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]{
switch email {
case dictionary["email"] as! String!:
results(true)
break
default:
results(false)
break
}
}
}, withCancel: nil)
}
可以这样调用该函数
checkIfUserExists(email: "some email", results:{ exists
if exists {
//do something
}
else{
// user do not exist... do something
}
})
@ohr答案应该是可以接受的,这只是一个澄清
@ohr answer should be the acepted one, this is only a clarification