Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajkumar17/9d0053a49470df23038050da326374dd to your computer and use it in GitHub Desktop.
Save rajkumar17/9d0053a49470df23038050da326374dd to your computer and use it in GitHub Desktop.
Example of using dispatch_once() in Swift
import Foundation
var token: dispatch_once_t = 0
func test() {
dispatch_once(&token) {
println("This is printed only on the first call to test()")
}
println("This is printed for each call to test()")
}
for _ in 0..<4 {
test()
}
/* Output:
This is printed only on the first call to test()
This is printed for each call to test()
This is printed for each call to test()
This is printed for each call to test()
This is printed for each call to test()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment