[swift-evolution] Removal of dispatch_once() in Swift 3?

Ben Rimmington me at benrimmington.com
Thu Jun 16 17:05:39 CDT 2016


William Shipley wrote:

> I may be missing something, but I don’t understand how to get the behavior
> of dispatch_once() without a bunch more code in cases in which I was using
> it to initialize “lazy-ish" instance variables.

In Objective-C, the `dispatch_once_t` predicate *must* be a static or global
variable (which is automatically initialized to zero).

<https://developer.apple.com/reference/dispatch/1447169-dispatch_once>

In any case, `dispatch_once` and `pthread_once` are not available. See the
"Interacting with C APIs" chapter in the pre-release "Using Swift with Cocoa
and Objective-C (Swift 3)".

<https://developer.apple.com/swift/resources/>

Note that "lazy stored properties" (i.e. `lazy var` instance variables)
are *not* safe to access by multiple threads simultaneously.

-- Ben


More information about the swift-evolution mailing list