Forked from jasonyunjoonpark/countAndUpdateUI.swift
Last active
April 28, 2018 20:15
-
-
Save iStefo/1868c80ad694efef76f689ef52984ee4 to your computer and use it in GitHub Desktop.
completion handler for a function that takes a parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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