简体   繁体   中英

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.

for example: I have two classes A and B. B has a method update and a variable updated.

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. 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

let obj_b = A()
obj_b.call()

Result Prints 1 , this is what you want the variable. You can use computed variable in such case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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