简体   繁体   English

在调用结束之前执行方法调用之后的语句

[英]statement after a method call gets executed before call ends swift

I am trying to call a method which is in another class and use a variable of that class which is getting updated in the method, after the method call.我正在尝试调用另一个类中的方法,并在方法调用后使用该类的变量,该变量在方法中得到更新。 But I am getting a nil value for the variable as the statement is getting called before the method executes completely.但是我得到了变量的 nil 值,因为在方法完全执行之前调用了该语句。

for example: I have two classes A and B. B has a method update and a variable updated.例如:我有两个类 A 和 B。B 有一个方法更新和一个变量更新。

class A {
    let obj_b = B()

    func call() {
        obj_b.update()
        let updated = obj_b.updated
    }
}

class B {
    var updated: Int?

    func updated() {
        updated = 1
    }
}

what I am trying is much more complex than the example and the method takes about 5secs to execute completely.我正在尝试的内容比示例复杂得多,并且该方法需要大约 5 秒才能完全执行。 Is there any way other than returning the variable that I need.除了返回我需要的变量之外,还有其他方法吗?

class A {
        let obj_b = B()

        func call() {
            let updated = obj_b.updatedVariable
            print("\(updated!)")
        }
    }

    class B {
        var updatedVariable: Int?{
            return  1
        }
    }

While calling A在调用 A 时

let obj_b = A()
obj_b.call()

Result Prints 1 , this is what you want the variable.结果打印 1 ,这就是你想要的变量。 You can use computed variable in such case.在这种情况下,您可以使用计算变量。

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

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