Forked from kristopherjohnson/dispatch_once.swift
Last active
September 8, 2015 03:38
-
-
Save ohtwo/9efb085837c2b42157d5 to your computer and use it in GitHub Desktop.
Example of using dispatch_once() in Swift
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 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