在使用User.provideInstance時出現問題,我無法初始化Self並返回。有什麼想法嗎?Swift在靜態方法中初始化自我
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
}
}
錯誤是let instance = self.init(managedObjectContext: context)
:
構建具有元類型值類型自我的對象必須使用需要初始化
你會得到什麼錯誤? – jtbandes
@jtbandes更新的問題,幷包含錯誤 – aryaxt