如何不断检查变量的值?

How do I check the value of a variable constantly?

Objective

我需要不断检查 Bool 变量的值,直到它的值为 true

代码

我尝试将动画中的 do-while 循环包含到将在运行时在 viewDidAppear 方法中调用的函数代码中。这工作正常,但不是一个有效的解决方案。

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

我也考虑过使用didSet属性,像这样:

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

问题

如何不断检查 Bool 变量的值?

您应该在 属性 中添加一个 属性 观察者。

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