Skip to content

Instantly share code, notes, and snippets.

@czars
Forked from kristopherjohnson/dispatch_once.swift
Created January 9, 2017 03:07
Show Gist options
  • Save czars/83efb512917e150c0977daa227790588 to your computer and use it in GitHub Desktop.
Save czars/83efb512917e150c0977daa227790588 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