Could you confirm that you have completed all these steps?
Step 1: Add Framework
Click on the application’s landing page (in the left pane its top icon with the name of your application), then go to the “Phase Assembly” tab, then in “Linking binary files to libraries”, click the small “+” number and then find “CoreData.framework "and add it to your project
Then either import coredata for all the objects that need it:
#import <CoreData/CoreData.h>
or add import under general import to your .pch file:
#ifdef __OBJC__ #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> #endif
Step 2: add a data model
To add the .xcdatamodel file, right-click / right-click on your files in the right pane (for example, in the "Resources" folder for safe storage) and select "Add New File". Go to the tab “Master data” when choosing a file type, then click “Data Model”, give it a name and click “Next” and “Finish”, and it will add it to your project. When you click on this model object, you will see that the interface will add objects to your project with any relationships you want.
Step 3: Update Application Delegate
Add these objects to AppDelegate.h
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; - (NSURL *)applicationDocumentsDirectory;
Synthesize previous objects in AppDelegate.m as follows:
@synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
Then add these methods to AppDelegate.m (do not forget to specify the name of the model that you added at the points shown):
- (void)saveContext{ NSError *error = nil; NSManagedObjectContext *managedObjectContext = self.managedObjectContext; if (managedObjectContext != nil) { if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } } - (NSManagedObjectContext *)managedObjectContext{ if (_managedObjectContext != nil) { return _managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { _managedObjectContext = [[NSManagedObjectContext alloc] init]; [_managedObjectContext setPersistentStoreCoordinator:coordinator]; } return _managedObjectContext; } - (NSManagedObjectModel *)managedObjectModel{ if (_managedObjectModel != nil) { return _managedObjectModel; } NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NAMEOFYOURMODELHERE" withExtension:@"momd"]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; return _managedObjectModel; } - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"NAMEOFYOURMODELHERE.sqlite"]; NSError *error = nil; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return _persistentStoreCoordinator; } #pragma mark - Application Documents directory // Returns the URL to the application Documents directory. - (NSURL *)applicationDocumentsDirectory{ return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; }
Step 4: Get Data Objects in ViewControllers Where You Need Data
in ViewController.h
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
In ViewController.m
@synthesize managedObjectContext = _managedObjectContext;
In the AppDelegate or class where the ViewController is created, set the managedObjectContext to the same as the AppDelegate
ViewController.managedObjectContext = self.managedObjectContext;
If you want the viewcontroller using Core Data to be a FetchedResultsController, you need to make sure that this material is in your ViewController.h
@interface ViewController : UIViewController <NSFetchedResultsControllerDelegate> { NSFetchedResultsController *fetchedResultsController; NSManagedObjectContext *managedObjectContext; } @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
And this is in ViewController.m
@synthesize fetchedResultsController, managedObjectContext;
Link: How to add Core Data to an existing project in Xcode
Hope it sheds light on your problem.