简体   繁体   English

快速关闭未设置变量

[英]Swift closure not setting variable

I'm trying to set a variable that is outside a closure inside that closure, but it doesn't end up getting set in the end. 我试图设置一个变量,该变量位于该闭包内部的闭包外部,但最终不会被设置。 However, the value that I'm setting the variable to is being printed to the console. 然而,为了被打印到控制台我设置的变量的值。 Also, right after setting the return variable and printing it itself, the correct value is being printed to the console. 同样,在设置返回变量并自行打印之后,将正确的值打印到控制台。 The problem arises when I return the variable; 当我返回变量时就会出现问题。 its value stays the same as the value at initialization. 它的值与初始化时的值相同。 Here is some psuedo-code: 这是一些伪代码:

let str: String = {
    var ret: String = "default value"

    functionWithClosure(with: (some_name) in {
        if let name = some_name {
            ret = name
            print(name) // prints successfully
            print(ret_name) // also prints successfully
        }
    })

    return ret // returns "default value"
}()

This is the actual code that isn't working: 这是不起作用的实际代码:

let name: String = {
    var ret_name = "default value"

    if let uid = FIRAuth.auth()?.currentUser?.uid {
        FIRDatabase.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
            if let dictionary = snapshot.value as? [String: AnyObject] {
                if let name = dictionary["name"] as? String {
                    ret_name = name
                    print(ret_name)
                }
            }
        })
    }

    return ret_name
}()

.observeSingleEvent is working async. .observeSingleEvent正在异步工作。

You can do something like this: 您可以执行以下操作:

func getRetname(completion: @escaping(_ retName: String) -> Void) {
    if let uid = FIRAuth.auth()?.currentUser?.uid {
        FIRDatabase.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            if let name = dictionary["name"] as? String {
                ret_name = name
                print(ret_name)
                completion(ret_name)
            }
        }
    })
}

Then, you can use it everywhere you want: 然后,您可以在任何需要的地方使用它:

getRetname(completion: { ret_name in 
    // ret_name - your data
})

Hope it helps 希望能帮助到你

May be below can be the workout for the issue. 可能是下面的问题的解决方案。

func getName(completion: @escaping(_ name: String) -> Void) {
if let uid = FIRAuth.auth()?.currentUser?.uid {
    FIRDatabase.database().reference().child("users")
    .child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
    if let dictionary = snapshot.value as? [String: AnyObject] {
        if let name = dictionary["name"] as? String {
            completion(name)
        }
    }
})
}

Now set the value of string by below code 现在通过以下代码设置字符串的值

getName(completion: { name in 
     let str = name
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM