iOS 开发 iOS Developer

iOS Swift dispatch_once

2016-05-12  本文已影响303人  云抱住阳光太阳没放弃发亮
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()
上一篇 下一篇

猜你喜欢

热点阅读