CoreData error: Failed to call designated initializer on NSManagedObject class

By | March 11, 2019

When you will get this error?

It’s a very common error who just started using Core Data in their iOS development journey. It comes when you create an instance of the Core Data class. e.g.
var items = Item() //Here Item is a Core Data class.

Reason for this error

In Core Data, Creating a direct instance of the Class is not allowed. We must use the designated initializer. e.g.
Item(context: NSManagedObjectContext)
Item(entity: NSEntityDescription, insertInto: NSManagedObjectContext?)

Solution for this error

Don’t create a direct instance of the Core Data Class.
1) Create an instance using the designated initializer
2) Create an Optional type, if you are not using the object right away.
var item : [Item]()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.