Reputation: 151
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
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
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