美文网首页
Swift-单例

Swift-单例

作者: iVikings | 来源:发表于2022-05-07 09:42 被阅读0次
    • 继承自 NSObject
    @objcMembers
    public class Singleton: NSObject {
        public static let shared = Singleton()
        
        private override init() { }
        
        public override func copy() -> Any {
            return self
        }
        
        public override func mutableCopy() -> Any {
            return self
        }
        
        // Optional
        public func reset() {
            
        }
    }
    
    • 不继承自 NSObject
    class Singleton {
        
        static let shared = Singleton()
        
        // Make sure the class has only one instance
        // Should not init outside
        private init() {}
        
        // Optional
        func reset() {
            // Reset all properties to default value
        }
    }
    

    相关文章

      网友评论

          本文标题:Swift-单例

          本文链接:https://www.haomeiwen.com/subject/bliturtx.html