#44: Singletons without dispatch_once in Swift
You no longer need dispatch_once to build a singleton in Swift. A static let property is lazy and thread-safe by default, so the old Objective-C pattern collapses into a tiny implementation:
final class APIClient {
static let shared = APIClient()
private init() {}
}
The important detail is that initialization happens only once, even if multiple threads try to access shared at the same time.
Keep the initializer private to prevent accidental extra instances, and use singletons sparingly since dependency injection often leads to more testable code.