I created a simple singleton in Swift 3:
class MySingleton {
private var myName: String
private init() {}
static let shared = MySingleton()
func setName(_ name: String) {
myName = name
}
func getName() -> String {
return myName
}
}
Since I made init()private and also declared an sharedinstance static let, I think the initializer is thread safe. But what about the getter and setter functions for myName, are they safe?
source
share