有人可以告诉我为什么我不断收到错误& quot;无效函数中的非预期非无效返回值". [英] Can someone tell me why I keep getting error "unexpected non-void return value in void function"

查看:45
本文介绍了有人可以告诉我为什么我不断收到错误& 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

这篇关于有人可以告诉我为什么我不断收到错误& quot;无效函数中的非预期非无效返回值".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆