Skip to content

Instantly share code, notes, and snippets.

@ohtwo
Forked from kristopherjohnson/dispatch_once.swift
Last active September 8, 2015 03:38
Show Gist options
  • Save ohtwo/9efb085837c2b42157d5 to your computer and use it in GitHub Desktop.
Save ohtwo/9efb085837c2b42157d5 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