UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.