Skip to content

Instantly share code, notes, and snippets.

@JARinteractive
Last active September 4, 2022 14:40
Show Gist options
  • Save JARinteractive/7fb33b6b0043f365ddfd to your computer and use it in GitHub Desktop.
Save JARinteractive/7fb33b6b0043f365ddfd to your computer and use it in GitHub Desktop.
wait for condition to be true
import Foundation
func waitUntil(@autoclosure checkSuccess: ()->Bool) -> Bool {
return waitUntil(1.0, checkSuccess)
}
func waitUntil(timeout: Double, @autoclosure _ checkSuccess: ()->Bool) -> Bool {
let startDate = NSDate()
var success = false
while !success && abs(startDate.timeIntervalSinceNow) < timeout {
NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.01))
success = checkSuccess()
}
return success
}
@marksands
Copy link

Add @discardableResult if you want those missing assignment warnings to go away

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment