when - Cannot create a managed object context on iOS




when to use core data (2)

I created a non core data project. I now want to use core data. In the build phases, I linked my binary with CoreData.framework. In my application delegate method, I want to manually create a managed object context like so

NSManagedObjectContext *aContext = [[NSManagedObjectContext alloc] init];

When I do the above, I get the following error,

Receiver 'NSManagedObjectContext' for class message is a forward declaration.

Any suggestions on what I might be doing wrong?


Just write #import < CoreData/CoreData.h > in your implementation file. It will work


You need to import CoreData/CoreData.h in your application delegate's header file:

#import <CoreData/CoreData.h>

Since you probably use it through outyour application you should put it in the precompiled header file, YourApp-Prefix.pch:

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




core-data