https://github.com/hpique/SwiftSingleton
An exploration of the Singleton pattern in Swift
https://github.com/hpique/SwiftSingleton
Last synced: 5 months ago
JSON representation
An exploration of the Singleton pattern in Swift
- Host: GitHub
- URL: https://github.com/hpique/SwiftSingleton
- Owner: hpique
- License: apache-2.0
- Created: 2014-06-10T04:57:25.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-11-05T18:02:48.000Z (about 9 years ago)
- Last Synced: 2024-11-19T23:58:20.323Z (about 1 year ago)
- Language: Swift
- Size: 250 KB
- Stars: 1,133
- Watchers: 39
- Forks: 101
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swift-and-tutorial-resources - SwiftSingleton - an exploration of the Singleton pattern in Swift (Swift Courses)
README
SwiftSingleton
==============
_tl;dr: Use the **class constant** approach if you are using Swift 1.2 or above and the **nested struct** approach if you need to support earlier versions._
An exploration of the Singleton pattern in Swift. All approaches below support lazy initialization and thread safety.
Issues and pull requests welcome.
### Approach A: Class constant
```swift
class SingletonA {
static let sharedInstance = SingletonA()
init() {
println("AAA");
}
}
```
This approach supports lazy initialization because Swift lazily initializes class constants (and variables), and is thread safe by the definition of `let`.
Class constants were introduced in Swift 1.2. If you need to support an earlier version of Swift, use the nested struct approach below or a global constant.
### Approach B: Nested struct
```swift
class SingletonB {
class var sharedInstance: SingletonB {
struct Static {
static let instance: SingletonB = SingletonB()
}
return Static.instance
}
}
```
Here we are using the static constant of a nested struct as a class constant. This is a workaround for the lack of static class constants in Swift 1.1 and earlier, and still works as a workaround for the lack of static constants and variables in functions.
### Approach C: dispatch_once
The traditional Objective-C approach ported to Swift.
```swift
class SingletonC {
class var sharedInstance: SingletonC {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: SingletonC? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = SingletonC()
}
return Static.instance!
}
}
```
I'm fairly certain there's no advantage over the nested struct approach but I'm including it anyway as I find the differences in syntax interesting.