Skip to content

Instantly share code, notes, and snippets.

@dlo
Forked from kristopherjohnson/dispatch_once.swift
Created June 20, 2016 01:43
Show Gist options
  • Save dlo/e5c91d063bde12a7fe7db54a32d9c08b to your computer and use it in GitHub Desktop.
Save dlo/e5c91d063bde12a7fe7db54a32d9c08b 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