GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Updating view with each individual loop cycle

Forums > macOS

I have a for...... loop. In each cycle a random set of numbers is generated. After each cycle I want a view to update, but what happens is that the only time it is updated is at the and of the loop. In IOS there is the ForEach loop. Is there something similar for MacoS programming. My code is meant to test some of the features I like to incorporate in a bigger program and I'm stuck with this...

I put in the sleep(1) Unix Command to monitor what's going on The levelValue tuple is used to set new heights to 2 rects that are being draw in the meterView. The corresponding NSBezierPaths are set in the meterView class (subclass of NSView) definition. This code is written in the viewcontroller and goRandom corresponds with a button:

@IBAction func goRandum(_ sender: Any) {
    for _ in 0...10 {
        meterView.levelValue = (rand(), rand())
        meterView.needsDisplay = true
        sleep(1)
        print(meterView.levelValue)
    }
}

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.