DarkLeafyGreen
DarkLeafyGreen

Reputation: 70466

Init method for core data entity not available

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

enter image description here

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:

enter image description here

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

Upvotes: 3

Views: 2626

Answers (1)

DarkLeafyGreen
DarkLeafyGreen

Reputation: 70466

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.

Upvotes: 6

Related Questions