ID declaration error when adding Core Data to an existing Xcode project

I have an existing project and I want to use CoreData.

After creating the project, CoreData.framework has already been added to my Frameworks group, and it is under Link Binary With Libraries in my Target β†’ Build Phases project. I did not check β€œUse basic data” when I created this project - the checkbox was even there - it was just in my project. I am using Xcode version 4.6.3.

Reading the tutorials, I went to my App-Prefix.pch and added import to CoreData. Now it looks like this:

 #import <Availability.h> #ifndef __IPHONE_5_0 #warning "This project uses features only available in iOS SDK 5.0 and later." #endif #ifdef __OBJC__ #import <UIKit/UIKit.h> #import <CoreData/CoreData.h> #import <Foundation/Foundation.h> #endif 

Then I added the following to AppDelegate.h :

 @property (readonly, nonatomic, strong) NSManagedObjectContext *managedObjectContext; @property (readonly, nonatomic, strong) NSManagedObjectModel *managedObjectModel; @property (readonly, nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator; - (void)saveContext; 

And now, when I override getter for managedObjectContext , Xcode throws an error:

Using the undeclared identifier "_managedObjectContext"; did you mean 'NSManagedObjectContext'?

This is my getter method in AppDelegate.m :

 - (NSManagedObjectContext *)managedObjectContext { if(_managedObjectContext != nil) return _managedObjectContext; NSPersistentStoreCoordinator* psc = [self persistentStoreCoordinator]; if(psc != nil) { _managedObjectContext = [[NSManagedObjectContext alloc] init]; [_managedObjectContext setPersistentStoreCoordinator:psc]; } return _managedObjectContext; } 

I also tried putting the .pch file in my resources to copy the bundle, but to no avail. Help?

+4
source share
2 answers

You configured everything correctly (note that you do not need to add PCH to the build phase of copy resources). The reason you get this error is because _managedObjectContext ivar is not synthesized because you override getter on a read-only property. You need to either change the property so that it is readwrite (which I would not recommend), override the property as readwrite in the class extension, or define ivar manually in the class extension or implementation block.

+6
source

For readonly properties that you even implement, there is no Ivar created by the compiler. Declare a variable

NsmanagedObjectContext * _managedObjectContext;

+1
source

Source: https://habr.com/ru/post/1495893/


All Articles