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

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:





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.