why am I getting an "unknown type name NSManagedObjectContext" in this code?
28,791
Solution 1
You then have to import the Core Data framework headers into any files that use Core Data classes.
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
Solution 2
As Greg said above, the way XCode 4 handles this is by adding #import <CoreData/CoreData.h>
into the [projectname]_Prefix.pch
file which I found in Other Sources folder/group.
Author by
Greg
Updated on July 10, 2022Comments
-
Greg almost 2 years
any ideas why am I getting an "unknown type name NSManagedObjectContext" in this code?
I'm basically adding core data to an existing project. I've added the coredata lines + I have added in the CoreData.framework to the project. To do this I went:
- when to application target
- build phases
- link binary with library
- then added the CoreData framwork
- then dragged it down on the project navigator so it appeared with the other framework icons in Xcode
Note sure what else I have to do? The CoreDataBooks example code that looks pretty much the same as what I have seems to compile
#import <UIKit/UIKit.h> @interface myAppAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; UINavigationController *navigationController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; // Core Data @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; // ERROR: unknown type @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; // ERROR: unknown type @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; // ERROR: unknown type - (void)saveContext; - (NSURL *)applicationDocumentsDirectory; @end
-
Greg about 13 yearsoh - I went back to the template that XCode 4 creates for a CoreData window based iPhone app and see, whilst there is no import like you suggest, there is this "#ifdef OBJC" section in a project-Prefix.pch file that does have the CoreData import statement in it
-
joshaidan almost 12 yearsPutting the declaration in the project-Prefix.pch file as Greg mentioned, I think is the better approach.
-
edzio27 over 11 yearsRemember also to add CoreData framework in project Build Phases.
-
jheriko almost 9 years-1 This is worst practice. include headers only where you need them, not everywhere. its a shame that apple provide this prefix header out of the box. the correct use for it is to improve compile times... not to hide dependencies.