I'd like to add core data to an existing iPhone project, but I still get a lot of compile errors:
- NSManagedObjectContext undeclared
- Expected specifier-qualifier-list before 'NSManagedObjectModel'
- ...
I already added the Core Data Framework to the target (right click on my project under "Targets", "Add" - "Existing Frameworks", "CoreData.framework").
My header-file:
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
[...]
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
What am I missing? Starting a new project is not an option...
Thanks a lot!
edit
sorry, I do have those implementations... but it seems like the Library is missing... the implementation methods are full with compile error like "managedObjectContext undeclared
", "NSPersistentStoreCoordinator undeclared
", but also with "Expected ')' before NSManagedObjectContext
" (although it seems like the parenthesis are correct)...
#pragma mark -
#pragma mark Core Data stack
/**
Returns the managed object context for the application.
If the context doesn't already exist, it is created and bound to the persistent store
coordinator for the application.
*/
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created by merging all of the models found in
application bundle.
*/
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return managedObjectModel;
}
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"Core_Data.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should
not use this function in a shipping application, although it may be useful during
development. If it is not possible to recover from the error, display an alert panel that
instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object
model
Check the error message to determine what the actual problem was.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
view.h
detail.h
sample coding view1
sample detail view
savebutton
For Swift 3:
File->new file->CoreData->Model to create a model.
Refer to this link for more information on how to implement it.
All the CoreData header files are imported in
App_Prefix.pch
, so the CoreData classes will be available throughout your Project, so you don't have to manually import the header in the files you need them.So open up Xcode and look for some file like
App_Prefix.pch
, by default it's in theOther Sources
group. After theUIKit
import statement, add the following line:And you should be ready to go.
Xcode 4
For projects created in Xcode 4, the prefix file can be found in the
Supporting Files
group in the Project navigator. It's called 'projectname-Prefix.pch' by default.Xcode 6+
Starting with Xcode 6, the precompiled header file is no longer included by default. This is because of the introduction of Modules, which take away the need to use precompiled headers. While it is still possible to manually add a PCH file to globally include the CoreData headers, consider specifying the CoreData dependency using
@import CoreData;
* in every file that uses CoreData. This makes dependencies explicit and more importantly will avoid this question's problem in the future.* Modules need to be enabled for this to work.
For Swift 3: INCLUDES SAVING AND RETRIEVING DATA
Step 1: Add Framework
Step 2: Add Data model
File > New > File > Core Data > Data Model
SampleData
the resultant file would beSampleData.xcdatamocelId
Step 3: Add the below functions to your App Delegate and add "import CoreData" to the top
STEP 4: Adding Entity and Attribute to the Model
a) Add Entity
b) Add Attribute
STEP 5: Saving Data
STEP 5: Retrieving Data