将Core Data添加到现有iPhone项目 [英] Adding Core Data to existing iPhone project

查看:80
本文介绍了将Core Data添加到现有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添加到目标中了(在Targets,Add - Existing Frameworks,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...

非常感谢!

/ strong>
对不起,我有那些实现...但似乎库缺少...实现方法充满编译错误像 managedObjectContext undeclared NSPersistentStoreCoordinator undeclared ,但也用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 ,默认情况下它在其他来源组。在 UIKit import语句之后,添加以下行:

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中创建的项目,前缀文件可以在支持文件组中找到在项目导航器中。默认情况下名为 projectname -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.

*模块需要启用才能正常工作。

* Modules need to be enabled for this to work.

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

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