In my AppDelegate.m, in the application: didFinishLaunchingWithOptions:
method, I put the following line:
NSManagedObjectContext *context = [self managedObjectContext];
But it says: AppDelegate may not respond to managedObjectContext. I saw this开发者_如何学JAVA in a tutorial online, what am I doing wrong? I put #import <CoreData/CoreData.h>
in my App_Prefix.pch file (see Adding Core Data to existing iPhone project) but that didn't help.
The goal is to then set myViewController.context = context
and then use that context to fetch some data in the view controller.
EDIT: Please see my comment to the answer of O. Begemann.
Create an empty sample app and make sure you check the Core Data checkbox. Then look at the boilerplate code for Core Data that has been generated in the application delegate. You need corresponding pieces of code in your app.
It is likely that the tutorial you are looking at used an iPhone project template that included Core Data. When you create a new project, most templates have a checkbox option to "Use Core Data for storage". Selecting that option creates three methods in your app delegate to retrieve a managedObjectContext, managedObjectModel and persistentStoreCoordinator. You would access those methods using [self managedObjectContext] etc, like in the tutorial you mention.
If you decide to add core data to an existing project and you didn't check off that box mentioned in the tutorial, you'll want to add the properties into the appdelegate header file as well as this important piece in your prefix.pch
#import <Availability.h>
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#endif
Do you have a method with the signature -(NSManagedObjectContext *) managedObjectContext;
or a @property(...) NSManagedObjectContext *managedObjectContext;
in your AppDelegate.h?
Tim,
Thanks a lot for your suggestion. After creating this boilerplate app I have realized that XCode generates some additional code in the app delegate IF you select CoreData option. I could not understand why Apple's dev guide has relatively long multi-step process for initializing the core data stack and most of the examples just refer to this (non-existing by default!) property. It turns out that all these examples assume that the application was created in certain way.
精彩评论