NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills 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)
    }
}

1      

Hacking with Swift is sponsored by MadMachine

SPONSORED Want to explore your Swift skill outside of the Apple world? Join the MadMachine community and start to program microcontrollers in Swift.

Get it 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.