将核心数据添加到现有的 iPhone 项目 [英] Adding Core Data to existing iPhone project

查看:23
本文介绍了将核心数据添加到现有的 iPhone 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to add core data to an existing iPhone project, but I still get a lot of compile errors:

- NSManagedObjectContext undeclared

 - Expected specifier-qualifier-list before 'NSManagedObjectModel'

 - ...

我已经将 Core Data Framework 添加到目标(在目标"下右键单击我的项目,添加"-现有框架",CoreData.framework").

I already added the Core Data Framework to the target (right click on my project under "Targets", "Add" - "Existing Frameworks", "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;

我错过了什么?开始一个新项目不是一种选择...

What am I missing? Starting a new project is not an option...

非常感谢!

编辑抱歉,我确实有这些实现......但似乎库丢失了......实现方法充满了编译错误,如managedObjectContext undeclared"、NSPersistentStoreCoordinator undeclared"code>",但在 NSManagedObjectContext 之前还有Expected ')'"(虽然看起来括号是正确的)...

edit sorry, I do have those implementations... but it seems like the Library is missing... the implementation methods are full with compile error like "managedObjectContext undeclared", "NSPersistentStoreCoordinator undeclared", but also with "Expected ')' before NSManagedObjectContext" (although it seems like the parenthesis are correct)...

#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 类将在您的整个项目中可用,因此您不必不必在您需要的文件中手动导入标题.

All the CoreData header files are imported in App_Prefix.pch, so the CoreData classes will be available throughout your Project, so you don't have to manually import the header in the files you need them.

因此,打开 Xcode 并查找诸如 App_Prefix.pch 之类的文件,默认情况下它位于 Other Sources 组中.在 UIKit 导入语句之后,添加以下行:

So open up Xcode and look for some file like App_Prefix.pch, by default it's in the Other Sources group. After the UIKit import statement, add the following line:

#import <CoreData/CoreData.h>

你应该准备好了.

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

For projects created in Xcode 4, the prefix file can be found in the Supporting Files group in the Project navigator. It's called 'projectname-Prefix.pch' by default.

从 Xcode 6 开始,默认情况下不再包含预编译头文件.这是因为引入了模块,消除了使用预编译头的需要.虽然仍然可以手动添加 PCH 文件以全局包含 CoreData 标头,考虑在每个使用 CoreData 的文件中使用 @import CoreData;* 指定 CoreData 依赖项.这使得依赖变得明确,更重要的是将来会避免这个问题的问题.

Starting with Xcode 6, the precompiled header file is no longer included by default. This is because of the introduction of Modules, which take away the need to use precompiled headers. While it is still possible to manually add a PCH file to globally include the CoreData headers, consider specifying the CoreData dependency using @import CoreData;* in every file that uses CoreData. This makes dependencies explicit and more importantly will avoid this question's problem in the future.

* 模块需要为此启用工作.

这篇关于将核心数据添加到现有的 iPhone 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆