FREE TRIAL: Accelerate your app development career with 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


Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

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

Reply to this topic…

You need to create an account or log in to reply.

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.