我从哪里获得托管对象上下文?

时间:2021-12-26 05:26:35

In my AppDelegate.m, in the application: didFinishLaunchingWithOptions: method, I put the following line:

在我的AppDelegate.m中,在应用程序:didFinishLaunchingWithOptions:方法中,我输入以下行:

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.

但它说:AppDelegate可能无法响应managedObjectContext。我在网上看到了这个,我做错了什么?我将#import 放在我的App_Prefix.pch文件中(请参阅向现有的iPhone项目添加核心数据),但这没有帮助。

The goal is to then set myViewController.context = context and then use that context to fetch some data in the view controller.

目标是然后设置myViewController.context = context,然后使用该上下文在视图控制器中获取一些数据。

EDIT: Please see my comment to the answer of O. Begemann.

编辑:请参阅我对O. Begemann答案的评论。

5 个解决方案

#1


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.

创建一个空的示例应用程序,并确保选中Core Data复选框。然后查看已在应用程序委托中生成的Core Data的样板代码。您需要在应用中使用相应的代码片段。

#2


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.

您正在查看的教程可能使用了包含Core Data的iPhone项目模板。创建新项目时,大多数模板都有一个“使用核心数据进行存储”的复选框选项。选择该选项会在应用程序委托中创建三个方法,以检索managedObjectContext,managedObjectModel和persistentStoreCoordinator。您可以使用[self managedObjectContext]等访问这些方法,就像您提到的教程一样。

#3


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

如果您决定将核心数据添加到现有项目并且没有勾选教程中提到的那个框,您将需要将属性添加到appdelegate头文件以及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

#4


0  

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

你有一个带签名的方法 - (NSManagedObjectContext *)managedObjectContext;或@property(...)NSManagedObjectContext * managedObjectContext;在你的AppDelegate.h?

#5


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.

非常感谢你的建议。创建这个样板应用程序后,我意识到如果您选择CoreData选项,XCode会在应用程序委托中生成一些额外的代码。我无法理解为什么Apple的开发指南具有相对较长的多步骤来初始化核心数据堆栈,而大多数示例只是引用这个(默认情况下不存在!)属性。事实证明,所有这些示例都假定应用程序是以某种方式创建的。

#1


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.

创建一个空的示例应用程序,并确保选中Core Data复选框。然后查看已在应用程序委托中生成的Core Data的样板代码。您需要在应用中使用相应的代码片段。

#2


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.

您正在查看的教程可能使用了包含Core Data的iPhone项目模板。创建新项目时,大多数模板都有一个“使用核心数据进行存储”的复选框选项。选择该选项会在应用程序委托中创建三个方法,以检索managedObjectContext,managedObjectModel和persistentStoreCoordinator。您可以使用[self managedObjectContext]等访问这些方法,就像您提到的教程一样。

#3


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

如果您决定将核心数据添加到现有项目并且没有勾选教程中提到的那个框,您将需要将属性添加到appdelegate头文件以及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

#4


0  

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

你有一个带签名的方法 - (NSManagedObjectContext *)managedObjectContext;或@property(...)NSManagedObjectContext * managedObjectContext;在你的AppDelegate.h?

#5


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.

非常感谢你的建议。创建这个样板应用程序后,我意识到如果您选择CoreData选项,XCode会在应用程序委托中生成一些额外的代码。我无法理解为什么Apple的开发指南具有相对较长的多步骤来初始化核心数据堆栈,而大多数示例只是引用这个(默认情况下不存在!)属性。事实证明,所有这些示例都假定应用程序是以某种方式创建的。