How to fix Core Data error: “Receiver type xxx for instance message is a forward declaration”

- by

Core Data is great. But when you add it to an existing project Xcode throws some random error messages at you that make you doubt your sanity. Again.

Legitimately copied and pasted code from Apple’s own templates generates totally useless errors such as Receiver type ‘NSManagedObjectContext’ for instance message is a forward declaration. This isn’t a problem for Xcode when creating a new project that uses Core Data, with the very same code.

Thanks to this pre-processor macro all those error messages disappear as if by magic, and the project builds fine – just as it should. Add it to the complaining class somewhere near the top:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
#endif

Note that you also need to add the Core Data Framework to your project for Core Data to work. It seems obvious, but it’s easily forgotten.

Kudos to capikaw for the solution:



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

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