Cesare
Cesare

Reputation: 9419

How do I check the value of a variable constantly?

Objective

I need to constantly check the value of a Bool variable until its value is true.

Code

I tried including a do-while loop in an animation into the code of a function that will be called in the viewDidAppear method at runtime. This works fine but it's not an efficient solution.

var myVariable: Bool = false
UIView.animateWithDuration(100, animations: {
        do {
            println(myVariable)
        } while variable == false
})

I also considered to use the didSet property, like so:

    var myVariable: Bool = false {
       didSet {
         if myVariable {
           println("Mission completed! The variable is true")
         } else {
           println("Keep checking...")
         }
       }
    }

Question

How do I check the value of a Bool variable constantly?

Upvotes: 2

Views: 1455

Answers (1)

rdelmar
rdelmar

Reputation: 104082

You should add a property observer to your property.

var myVariable: Bool = false {
        willSet{
            if newValue == true {
                println("the Bool is now true")
            }
        }
    }

Upvotes: 5

Related Questions