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

前端之家收集整理的这篇文章主要介绍了如何在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.
原文链接:https://www.f2er.com/swift/320004.html

猜你在找的Swift相关文章