Singleton in Swift

后端 未结 4 1677
盖世英雄少女心
盖世英雄少女心 2020-11-28 08:45

I\'ve been trying to implement a singleton to be used as a cache for photos which I uploaded to my iOS app from the web. I\'ve attached three variants in the code below. I

4条回答
  •  孤街浪徒
    2020-11-28 09:18

    Swift-5

    To create a singleton class:

    import UIKit
    
    final class SharedData: NSObject {
       static let sharedInstance = SharedData()
    
       private override init() { }
    
       func methodName() { }
    }
    

    To access

    let sharedClass = SharedClass.sharedInstance
    

    OR

    SharedClass.sharedInstance.methodName()
    

提交回复
热议问题