嵌套函数中函数的返回值(Swift 3 | Xcode) [英] Return value for function in nested function (Swift 3 | Xcode)

查看:117
本文介绍了嵌套函数中函数的返回值(Swift 3 | Xcode)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从嵌套函数中返回一个函数的值。我明白为什么它不起作用,但我只需要知道我的问题类型的解决方法。



在我的代码中,我正在检查是否a人员已经在我的Firebase数据库中创建。因此,在usersRef函数中,您会看到如果从firebase返回的值为nil,那么用户尚未创建,反之亦然。

当我说: return userBool 它给了我这个错误:

 意外的非void函数返回值

一直在寻找一个干净的解决方法,但我唯一能想到的我不得不一遍又一遍地为每一次我想检查它创建这个函数。显然,我宁愿不这样做,哈哈,所以如果你能帮助我解决这个问题并提供一个干净的解决方案,请告诉我! :)

  func checkIfUserExists(userID:String) - > Bool {
var userBool = Bool()
var usersRef = FIRDatabase.database()。reference()
usersRef.child(users)。child(userID).observeSingleEvent(of: .value,其中:{
快照在
中,如果snapshot.value!= nil {
userBool = true
返回userBool
}
else {
userBool = false
return userBool
}
})
}

更新:

感谢您的建议,我添加了一个完成,并且完美运作。 :)


$ b

  func checkIfUserExists(userID:String,completion:@escaping(_ success:Bool) - >()){ 
var userBool = Bool()
var usersRef = FIRDatabase.database()。reference()
usersRef.child(users)。child(userID).observeSingleEvent(of:.value ,其中:{
快照在
中,如果snapshot.value!= nil {
print(userBool!,userBool)
userBool = true
}
else {
print(userBool2!,userBool)
userBool = false
}
completion(userBool)
})
}


解决方案

无法从内部的外部函数返回一。我能想到的唯一方法是使用完成处理程序,然后可以从嵌套函数内调用,如下所示:

  func checkIfUserExists(userID:String,completion:@escaping(_ userExists:Bool) - > Void)


I'm trying to return a value for a function from inside of a nested function. I understand why it doesn't work, but I just need to know a workaround for my type of problem.

In my code, I'm checking whether or not a person has already been created inside of my Firebase Database. So inside of the usersRef function, you'll see that if the value return from firebase is "nil" then the user hasn't been created and vice versa.

When I say : return userBool it's giving me this error:

Unexpected non-void return value in void function

Been looking for a clean workaround, but the only thing I can think of is having to create this function over and over again for each of the various times I want to check it. Obviously, I'd rather not do that, haha, so if you could help me out by giving some suggestions on how to fix this and have a clean solution, let me know! :)

func checkIfUserExists(userID : String) -> Bool {
    var userBool = Bool()
    var usersRef = FIRDatabase.database().reference()
    usersRef.child("users").child(userID).observeSingleEvent(of: .value, with: {
    snapshot in
        if snapshot.value != nil {
            userBool = true
            return userBool
        }
        else {
            userBool = false
            return userBool
        }
    })
}

UPDATE :

Thanks to your suggestions, I added a completion, and it worked perfectly. :)

func checkIfUserExists(userID : String, completion: @escaping (_ success: Bool) -> ()){
    var userBool = Bool()
    var usersRef = FIRDatabase.database().reference()
    usersRef.child("users").child(userID).observeSingleEvent(of: .value, with:{
        snapshot in
            if snapshot.value != nil {
                print("userBool!", userBool)
                userBool = true
            }
            else {
                print("userBool2!", userBool)
                userBool = false
            }
        completion(userBool)
    })
}

解决方案

There is no way to return from the outer function from within the inner one. The only way I can think of is by using a completion handler, that you can then call from within the nested function like so:

func checkIfUserExists(userID : String, completion: @escaping (_ userExists: Bool) -> Void)

这篇关于嵌套函数中函数的返回值(Swift 3 | Xcode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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