简体   繁体   中英

How to keep updating a constant value in Swift

I have a problem, I'm trying to attach a loader to the view and it's working fine but the conditions are looping...

var conditions: Bool?
loader.startLoader(view: view)
func loadingImages() {
        conditions = (button1.currentImage == nil) || (button2.currentImage == nil) || (button3.currentImage == nil) || (button4.currentImage == nil) || (button5.currentImage == nil) || (button6.currentImage == nil) || (button7.currentImage == nil) || (button8.currentImage == nil) || (button9.currentImage == nil)
        
        if let cond = conditions {
            loadingImages()
        } else {
            loader.stop()
            return
        }
    }

I'm trying to make the loader visible only when conditions are satisfied.

How can I make a function that ass soon as the conditions gets false the loader.stop get triggered? Because using a method as above it's getting into a infinite loop.

Thank you

You can stop it in did set method or in setter like this

var conditions: Bool? {
        didSet {
            if let condition = conditions , !condition {
                loader.stop()
            }
        }
    }

in this line, you just unwrapped the optional conditions , and nothing else. that's why it will cause an infinite loop. Because whether true or false, it's still valid as long as it has value.

if let cond = conditions {
            loadingImages()
        }

In order to solve your problem, you can just simply compare the conditions to true, like:

if conditions == true {
    loadingImages()
} else {
    loader.stop()
    return
}

Cheers, Kel

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