Swift initialize Self in a static methods -
having problem getting work, inside user.provideinstance i'm unable initialize self , return. thoughts?
extension nsmanagedobject { public convenience init(managedobjectcontext: nsmanagedobjectcontext) { let entity = nsentitydescription.entityforname(string(self.dynamictype), inmanagedobjectcontext: managedobjectcontext)! self.init(entity: entity, insertintomanagedobjectcontext: managedobjectcontext) } } public protocol deserializable { static func provideinstance(json: [nsobject: anyobject]) -> self } @objc(user) public class user: nsmanagedobject, deserializable { public static func provideinstance(json: [nsobject: anyobject]) -> self { let context = dicontainer.instance.resolve(coredatastack.self).managedobjectcontext let instance = self.init(managedobjectcontext: context) return instance } }
error on let instance = self.init(managedobjectcontext: context)
:
constructing object of class type self metatype value must use required initializer
why bother initializers?
extension nsmanagedobject { class func provide(managedobjectcontext managedobjectcontext: nsmanagedobjectcontext) -> self { let entity = nsentitydescription.entityforname(string(self.dynamictype), inmanagedobjectcontext: managedobjectcontext)! return self.init(entity: entity, insertintomanagedobjectcontext: managedobjectcontext) } } public protocol deserializable { static func provideinstance(json: [nsobject: anyobject]) -> self } @objc(user) public class user: nsmanagedobject, deserializable { public static func provideinstance(json: [nsobject: anyobject]) -> self { let context = ... let instance = self.provide(managedobjectcontext: context) return instance } }
the problem initializers not inherited, unless required
. , cannot create required
initializer in extension.
Comments
Post a Comment