Thomas Delputte
Thomas Delputte

Reputation: 151

NSEntityDescription is nil

My variable entityDescription seems to be nil, so I have a fatal error while compiling. Does anyone knows a solution? persistentContainer is declared in the same class (AppDelegate).

let entityDescription = NSEntityDescription.entity(forEntityName: "Person", in: self.persistentContainer.viewContext)
let newPerson = NSManagedObject(entity: entityDescription!, insertInto: self.persistentContainer.viewContext)
newPerson.setValue("Thomas", forKey: "first")

Upvotes: 2

Views: 3734

Answers (2)

CatVsDogs
CatVsDogs

Reputation: 374

Please check the method name, The following code creates a NSEntityDescription object for me.

let desc:NSEntityDescription? = NSEntityDescription.entityForName("Person", inManagedObjectContext: self.managedObjectContext);
print(desc)

Upvotes: 4

Mundi
Mundi

Reputation: 80265

You should have a look at the Core Data Xcode template. (Choose new project, check, Master/Detail, check Core Data.) In Swift 3 you would create a new object like by using a valid managed object context.

let newPerson = Person(context: context)
newPerson.first = "Thomas"

Upvotes: 2

Related Questions