According to this blog post and the currently highest voted answer to this Stack Overflow question, which in turn cites Apple's documentation, the best way to create a singleton in modern Swift is:
class Singleton {
static let sharedInstance = Singleton()
}
Although it wasn't mentioned, probably a private init()
is called for as well.
To me, a simpler alternative would be to convert all properties and methods to static
, and drop the sharedInstance
property.
For example, suppose I wrote a class with a property and a method, following the advice above, as follows:
class Singleton {
static let sharedInstance = Singleton("whatever")
var myProperty: String
func myMethod() {
// ...
}
private init(_ myProperty) {
self.myProperty = myProperty
}
}
If the user needs to access the property in question, they would write Singleton.sharedInstance.myProperty
, and if they need to call the method, they would write Singleton.sharedInstance.myMethod()
.
I propose rewriting the class as follows:
class Singleton {
static var myProperty: String = "whatever"
static func myMethod() {
// ...
}
}
So: less boilerplate code, and less characters to type when accessing the property (just Singleton.myProperty
) and method (Singleton.myMethod()
).
One disadvantage is that accesses to the property and method from inside the class would need to be fully spelled out (Singleton.myProperty
and Singleton.myMethod()
), compared to just myProperty
and myMethod()
for the previous solution.
Thus, it's a little bit easier for the user (dropping the sharedInstance
part) and a little bit harder for the class writer (which needs to add Singleton.
in front of all accesses). It seems reasonable that, when faced with a design choice that either favors the user or the class writer, the better option is to favor the user.
Nobody else appears to advocate the method I proposed for making a singleton, so I get the feeling there must be something wrong with it. Would someone be so kind as to point out what is it?