gpt4 book ai didi

swift - 有人能告诉我为什么我一直收到错误 "unexpected non-void return value in void function"

转载 作者:搜寻专家 更新时间:2023-11-01 07:20:07 26 4
gpt4 key购买 nike

尝试返回 true 或 false bool 值时出现错误...有人可以帮忙吗?我试图检查用户是否存在于 Firebase 数据库中。

    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, completion:(success:Bool) -> ())

然后一旦你的 FIRDatabase 闭包结束而不是返回 true 或 false 你

completion(success:true) // or false

然后像这样使用它。

checkIfUserExists("email@mail.com") { (success) in
if success
{
// Email found
}
else{

}
}

link可能会有帮助

关于swift - 有人能告诉我为什么我一直收到错误 "unexpected non-void return value in void function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com