import Foundation
struct Static {
static var dispatchOnceToken: dispatch_once_t = 0
}
func test() {
dispatch_once(&Static.dispatchOnceToken) {
print("This is printed only on the first call to test()")
}
print("This is printed for each call to test()")
}
测试:
for _ in 0..<4 {
test()
}
输出:
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()