Singletons in Swift 5

梦想与她 提交于 2019-12-08 15:33:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!