Search code examples
swiftwhile-loopboolean

More elegant way to wait for a boolean to be 'true'


I am a beginner in Swift, so this is how I wait for a boolean variable to be true:

while (boolVar == false) {}

I know that eventually, from another method, this variable will be set to true. Is there a better way to achieve the above? I'm not sure if that is the most elegant and/or efficient way.


Solution

  • The simple swifty way is to use didSet

    var boolVar:Bool = false {
        didSet {
           if boolVar {
              // do job
           } 
        } 
    }