Learn Courses My Dashboard

Cannot convert value of type 'NSPersistentContainer' to expected argument type 'NSManagedObjectContext'

Hello there,

I am at Module 6 / Lesson 3 of the data bases course and getting the above mentioned error after finishing the preloadLocalData function.
I would appreciate your help, thank you very much.

Your managedObjectContext is not, as you might expect from the name, an NSManagedObjectContext but rather an NSPersistentContainer. So you can’t pass it where an NSManagedObjectContext is expected.

You actually do use an NSManagedObjectContext a little later, in this line:

try managedObjectContext.viewContext.save()

viewContext is your actual NSManagedObjectContext.

Part of the problem here is the way you’ve named things. Don’t name something managedObjectContext if it’s not, in fact, an NSManagedObjectContext. That’s just confusing.

Thank you very much roosterboy. That helped me me a lot.