Skip to content

Instantly share code, notes, and snippets.

@iStefo
Forked from jasonyunjoonpark/countAndUpdateUI.swift
Last active April 28, 2018 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iStefo/1868c80ad694efef76f689ef52984ee4 to your computer and use it in GitHub Desktop.
Save iStefo/1868c80ad694efef76f689ef52984ee4 to your computer and use it in GitHub Desktop.
completion handler for a function that takes a parameter
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global(qos: .userInteractive).async {
//Background thread
// NOTE1: remove the `func` keyword and store the result
let result = countTo(10) //not sure how to wait for this to be finished before updating the button label on main thread
DispatchQueue.main.async {
//Update Button Label after count is finished
// NOTE2: This captures both the result and `self`, which is ok in this case
self.label.text = result
}
}
}
func countTo(number: Int) -> Int {
var counter = 0
while counter < number {
counter += 1
print(counter)
}
return counter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment