TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.