微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐