简体   繁体   English

将Core Data添加到现有Xcode项目时未声明的标识符错误

[英]Undeclared identifier error when adding Core Data to existing Xcode project

I have an existing project and I want to use CoreData. 我有一个现有的项目,我想使用CoreData。

Upon creation of the project, the CoreData.framework is already added under my Frameworks group, and it is under Link Binary With Libraries in my project's Target -> Build Phases. 创建项目后, CoreData.framework已添加到我的Frameworks组下,并且在我的项目的Target - > Build Phases中的Link Binary With Libraries下。 I didn't check "Use Core Data" when I created this project-- the check box was not even there --it was just simply in my project. 我创建这个项目时没有检查“使用核心数据” - 复选框甚至没有 - 只是在我的项目中。 I use Xcode version 4.6.3. 我使用Xcode版本4.6.3。

Reading the tutorials, I went to my App-Prefix.pch and added an import to CoreData. 阅读教程,我去了我的App-Prefix.pch并添加了一个导入到CoreData。 It now looks like this: 它现在看起来像这样:

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <CoreData/CoreData.h>
    #import <Foundation/Foundation.h>
#endif

Then, I added the following in my AppDelegate.h : 然后,我在AppDelegate.h添加了以下内容:

@property (readonly, nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (readonly, nonatomic, strong) NSManagedObjectModel *managedObjectModel;
@property (readonly, nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;

And now, when I override the getter for managedObjectContext , Xcode throws an error: 现在,当我覆盖managedObjectContext的getter时,Xcode会抛出一个错误:

Use of undeclared identifier '_managedObjectContext'; 使用未声明的标识符'_managedObjectContext'; did you mean 'NSManagedObjectContext'? 你的意思是'NSManagedObjectContext'?

This is my getter method in AppDelegate.m : 这是我在AppDelegate.m getter方法:

- (NSManagedObjectContext *)managedObjectContext {
    if(_managedObjectContext != nil)
        return _managedObjectContext;

    NSPersistentStoreCoordinator* psc = [self persistentStoreCoordinator];

    if(psc != nil)
    {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:psc];
    }

    return _managedObjectContext;
}

I also tried putting the .pch file in my Copy Bundle Resources but to no avail. 我也尝试将.pch文件放在我的Copy Bundle Resources中,但无济于事。 Help? 救命?

You've set everything up correctly (note that you don't need to add the PCH to the Copy Bundle Resources build phase). 您已正确设置所有内容(请注意,您无需将PCH添加到Copy Bundle Resources构建阶段)。 The reason you're getting that error is because the _managedObjectContext ivar is not getting synthesized, because you're overriding the getter on a read-only property. 您收到该错误的原因是因为_managedObjectContext ivar未被合成,因为您在只读属性上覆盖了getter。 You either need to change the property to be readwrite (which I wouldn't recommend), redefine the property as readwrite in a class extension, or define the ivar manually in a class extension or the implementation block. 您需要将属性更改为readwrite(我不建议这样做),在类扩展中将该属性重新定义为readwrite,或者在类扩展或实现块中手动定义ivar。

For readonly properties you Even implement there is no Ivar created by the compiler. 对于readonly属性,即使实现,编译器也不会创建Ivar。 Declare the Variable 声明变量

NsmanagedObjectContext *_managedObjectContext; NsmanagedObjectContext * _managedObjectContext;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM