Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create a managed object context on iOS

Tags:

ios

core-data

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?

like image 518
David Avatar asked Nov 10 '11 23:11

David


People also ask

What is managed object context?

A managed object context represents a single object space, or scratch pad, in a Core Data application. A managed object context is an instance of NSManagedObjectContext . Its primary responsibility is to manage a collection of managed objects.

Can we create multiple managed object context?

Most apps need just a single managed object context. The default configuration in most Core Data apps is a single managed object context associated with the main queue. Multiple managed object contexts make your apps harder to debug; it's not something you'd use in every app, in every situation.

What is NSManagedObjectContext in Swift?

An object space to manipulate and track changes to managed objects.

How do I set up NSManagedObject?

Creating NSManagedObject Subclasses To create a subclass of NSManagedObject , in the Xcode Core Data model editor, select the entity, and in the Entity pane of the Data Model inspector, enter the name in the Class field. Then create the subclass in Xcode.


2 Answers

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
like image 53
adam0101 Avatar answered Oct 02 '22 05:10

adam0101


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

like image 30
GameBegins Avatar answered Oct 02 '22 06:10

GameBegins