iOS Swift dispatch_once

作者: 云抱住阳光太阳没放弃发亮 | 来源:发表于2016-05-12 18:35 被阅读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()
    

    相关文章

      网友评论

        本文标题:iOS Swift dispatch_once

        本文链接:https://www.haomeiwen.com/subject/gxeprttx.html