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:





2 thoughts on “How to fix Core Data error: "Receiver type xxx for instance message is a forward declaration"

  1. Often this can also be resolved by correctly importing Core Data into the header file of the complaining class, like so:

    #import <CoreData/CoreData.h>
    

    Not doing so means Xcode doesn’t know any of the Core Data classes.

Leave a Reply