Init method for core data entity not available

穿精又带淫゛_ 提交于 2020-01-13 18:11:09

问题


I am on xcode 6.1 and developing for iOS 8.1. I have simple project called CoreDataTest where I play around with core data.

Model:

import CoreData

class Licence: NSManagedObject {
    @NSManaged var name: String
}

and

Now I want to create a licence object. This is what I need prior to creating the object:

let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!

let ent = NSEntityDescription.entityForName("Licence", inManagedObjectContext: context)

The problem is that xcode does not provide the correct constructor for the entity. When I start typing:

var newLicence = Licence(..

I get no auto completion like in this example:

I just get the NSManagedObject() as auto completion. Any ideas whats wrong?


回答1:


Solved it by reading the docs for NSManagedObject :)

It is important that a managed object is properly configured for use with Core Data. If you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:)

So in my class I implemented:

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

and now it works.



来源:https://stackoverflow.com/questions/26608056/init-method-for-core-data-entity-not-available

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