问题
Here's precisely how I make a singleton,
public class Model
{
static let shared = Model()
// For ocd friends. Add this line: private init() {}
func test()->Double
{
return 3.33
}
}
then elsewhere...
class ViewController:UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
print("Holy singleton test, Batman! \( Model.shared.test() )")
}
}
What about in Swift 5?
Any new dramas or insights? Have they perhaps added "actual" singletons, or?
回答1:
Nothing new. It remains the same in Swift 5.
来源:https://stackoverflow.com/questions/57188218/singletons-in-swift-5