7

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 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.

Community
  • 1
  • 1

5 Answers5

11

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.

Ole Begemann
  • 132,868
  • 29
  • 267
  • 251
  • 1
    Now I'm getting an exception: `*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'`, at the line: `NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"InfoWeek.sqlite"]]`. I checked in the applicationDocumentsDirectory; for the sample app there is a Untitled.sqlite file, but mine is empty. What should I do? –  Nov 10 '10 at 02:00
  • OK, resolved, it was actually a different line that was the problem and I needed to change `ofType:@"momd"` to "mom". –  Nov 13 '10 at 21:57
1

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
kevinl
  • 4,134
  • 6
  • 35
  • 52
1

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.

Tim Isganitis
  • 1,544
  • 9
  • 11
0

Do you have a method with the signature -(NSManagedObjectContext *) managedObjectContext; or a @property(...) NSManagedObjectContext *managedObjectContext; in your AppDelegate.h?

vikingosegundo
  • 51,126
  • 14
  • 131
  • 172
0

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.