如何在Swift中执行一次代码只执行一次?

到目前为止我所看到的答案( 1,2,3)建议使用GCD的dispatch_once:
var token: dispatch_once_t = 0
func test() {
    dispatch_once(&token) {
        print("This is printed only on the first call to test()")
    }
    print("This is printed for each call to test()")
}
test()

输出

This is printed only on the first call to test()
This is printed for each call to test()

但等一下. token是一个变量,所以我可以很容易地做到这一点:

var token: dispatch_once_t = 0
func test() {
    dispatch_once(&token) {
        print("This is printed only on the first call to test()")
    }
    print("This is printed for each call to test()")
}
test()

token = 0

test()

输出

This is printed only on the first call to test()
This is printed for each call to test()
This is printed only on the first call to test()
This is printed for each call to test()

因此,如果我可以更改令牌的值,dispatch_once是没用的!将令牌转换为常量并不简单,因为它需要类型为UnsafeMutablePointer< dispatch_once_t>.

那么我们应该放弃Swift中的dispatch_once吗?有一种更安全的方式只执行一次代码吗?

由闭包初始化的静态属性是懒惰运行的,最多只运行一次,所以这只打印一次,尽管被调用了两次:
/*
run like:

    swift once.swift
    swift once.swift run

to see both cases
*/
class Once {
    static let run: Void = {
        print("Behold! \(__FUNCTION__) runs!")
        return ()
    }()
}

if Process.arguments.indexOf("run") != nil {
    let _ = Once.run
    let _ = Once.run
    print("Called twice,but only printed \"Behold\" once,as desired.")
} else {
    print("Note how it's run lazily,so you won't see the \"Behold\" text Now.")
}

示例运行:

~/W/WhenDoesstaticDefaultRun> swift once.swift
Note how it's run lazily,so you won't see the "Behold" text Now.
~/W/WhenDoesstaticDefaultRun> swift once.swift run
Behold! Once runs!
Called twice,but only printed "Behold" once,as desired.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...