oop - Swift singleton subclass(inheritance) -
i know simplest way write singleton in swift is
class { static let shared = a() private init() { //... } func a() {} } // usage a.shared.a() the question is possible write subclass singleton of class a?
i got following code class function
class { class func shared() -> { private struct _a { static let _shared = a() } return _a.shared } func a() { //... } } class b: { class func shared() -> b { private struct _b { static let _shared = b() } return _b.shared } func b() { //... } } // usage a.shared.a() b.shared.b() but problem can't use private init() hide initialization here, since there no protected init() this
does know how subclass singleton class using private init()? thanks!
i'm not sure idea go down road. however, if must, keep them in same file , use fileprivate accomplish this.
in class declaration:
fileprivate init() { } in class b declaration:
override fileprivate init() { } good luck!
Comments
Post a Comment