Init method is not available for the main data object

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

Model:

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

and

enter image description here

Now I want to create a license object. This is what I need before 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 object. When I start typing:

 var newLicence = Licence(.. 

I do not get automatic completion, as in this example:

enter image description here

I just get NSManagedObject () as auto completion. Any ideas on something wrong?

+3
source share
1 answer

Solved it by reading docs for NSManagedObject :)

It is important that the managed entity is properly configured for use with master data. If you instantiate the managed entity 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.

+4
source

Source: https://habr.com/ru/post/977314/


All Articles