Swift闭包没有设置变量

时间:2017-04-21 04:44:29

标签: swift firebase firebase-realtime-database closures firebase-authentication

我试图在闭包内设置一个闭包之外的变量,但它最终还是没有设置好。但是,我将变量设置为的值正在打印到控制台。此外,在设置返回变量并自行打印之后,正在向控制台输出正确的值。当我返回变量时会出现问题;它的值与初始化时的值保持一致。这是一些伪代码:

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"
}()

这是不起作用的实际代码:

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
}()

2 个答案:

答案 0 :(得分:1)

.observeSingleEvent正在异步工作。

您可以这样做:

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)
            }
        }
    })
}

然后,您可以随意使用它:

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

希望有所帮助

答案 1 :(得分:1)

可能在下面可以解决问题。

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)
        }
    }
})
}

现在通过以下代码

设置字符串的值
getName(completion: { name in 
     let str = name
 })