Can't instantiate subclass of NSManagedObject

谁都会走 提交于 2019-12-03 14:20:04
Upvote

Just came across the same problem: Init method for core data entity not available

Obviously we have to implement the

init(entity: NSEntityDescription, insertIntoManagedObjectContext context, NSManagedObjectContext?)

method in our custom NSManagedObject class. So just add

override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
    super.init(entity: entity, insertIntoManagedObjectContext: context)
}

to your entity class and it will work.

Try the final line without exclamation mark, like this:

var item = Item(entity: ent, insertIntoManagedObjectContext: context)

And maybe You haven't added your app name to class name:

Swift classes are namespaced—they’re scoped to the module (typically, the project) they are compiled in. To use a Swift subclass of the NSManagedObject class with your Core Data model, prefix the class name in the Class field in the model entity inspector with the name of your module.

https://developer.apple.com/library/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html

Are constructors inherited in Swift?

I'd try using NSEntityDescription.insertNewObjectForEntityForName:inManagedObjectContext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!