我想将核心数据添加到现有的iPhone项目中,但仍然会遇到很多编译错误:

- NSManagedObjectContext undeclared

 - Expected specifier-qualifier-list before 'NSManagedObjectModel'

 - ...

我已经将核心数据框架添加到目标中(右键单击"Targets"、"Add"-"Existing Framework"、"CoreData.framework"下面的项目).

我的头文件:

NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;       
NSPersistentStoreCoordinator *persistentStoreCoordinator;

[...]

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

我错过了什么?启动一个新项目不是一个选项...

谢谢!

edit 抱歉,我确实有这些实现.但看起来图书馆好像不见了...实现方法充满了编译错误,如"managedObjectContext undeclared"、"NSPersistentStoreCoordinator undeclared",但在NSManagedObjectContext之前也有"预期的‘)’"(尽管看起来括号是正确的)……

#pragma mark -
#pragma mark Core Data stack

/**
 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store         
coordinator for the application.
 */
- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}


/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in    
 application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}


/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] 
        stringByAppendingPathComponent: @"Core_Data.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] 
    initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
    configuration:nil URL:storeUrl options:nil error:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should 
    not use this function in a shipping application, although it may be useful during 
    development. If it is not possible to recover from the error, display an alert panel that 
    instructs the user to quit the application by pressing the Home button.

     Typical reasons for an error here include:
     * The persistent store is not accessible
     * The schema for the persistent store is incompatible with current managed object 
                model
     Check the error message to determine what the actual problem was.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return persistentStoreCoordinator;
}

推荐答案

所有的CoreData头文件都是在App_Prefix.pch中导入的,因此CoreData类将在整个项目中可用,因此您不必在需要它们的文件中手动导入头文件.

所以打开Xcode,寻找像App_Prefix.pch这样的文件,默认情况下,它在Other Sources组中.在UIKit import语句之后,添加以下行:

#import <CoreData/CoreData.h>

你应该准备好出发了.

Xcode 4

对于用Xcode4创建的项目,可以在Project导航器的Supporting Files组中找到前缀文件.默认情况下,它的名称为‘projectname-Prefix.pch’.

Xcode 6+

从Xcode6开始,默认情况下不再包含预编译头文件.这是因为引入了模块(Modules),其中take away the need to use precompiled headers.虽然仍然可以手动添加PCH文件以全局包含CoreData标头,但可以考虑在每个使用CoreData的文件中使用@import CoreData;*指定CoreData依赖项.这使得依赖关系变得明确,更重要的是可以避免将来出现这个问题.

*模块need to be enabled使其工作

Ios相关问答推荐

SWIFT中具有可选返回类型和泛型的网络请求

在SwiftUI中使用系统图像和色调创建圆形按钮

如何在不将应用程序留在SwiftUI上的情况下更改语言?

Flutter iOS 键盘问题:. TextInputType.number 中缺少字符

每次我路由到应用程序的主屏幕时,都无法使组合订阅正常工作

使用按位|带有布尔操作数

具有一个参数/参数的泛型类的泛型类

CoreML 不能并发(多线程)工作?

无法为 Flutter 项目构建 ipa [CocoaPods 找不到 pod "GoogleAppMeasurement" 的兼容版本:]

使用swift将图像转换为A4大小的pdf

通过按钮打开 AppStore

在iOS7半透明导航栏中获取正确的 colored颜色

光标未显示在 UITextView 中

UICollectionView - 动态单元格高度?

在 Swift 中上传带参数的图像

检测 iOS UIDevice 方向

如何将自己项目中的类导入 Playground

iOS:以编程方式制作屏幕截图的最快、最高效的方法是什么?

'无效更新:第 0 节中的无效行数

为什么 Xcode 4 不创建任何产品?