Cannot create a managed object context on iOS

IosCore Data

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - Iosadam0101View Answer on Stackoverflow
Solution 2 - IosGameBeginsView Answer on Stackoverflow