gar*_*hdn 4 iphone core-data objective-c ios
我正在尝试了解Core Data,并且正在关注Apple的"Locations"教程.我遇到的一个直接绊脚石是我已经开始构建我想要使用Core Data的应用程序.它是单一视图应用程序.
为了在此应用程序中使用Core Data,我需要采取哪些步骤?教程说在创建项目时选择"使用核心数据进行存储"复选框,但必须有一种方法可以在项目创建后启用核心数据.
我真的很感激一些帮助.谢谢.
rul*_*ule 14
1.)创建一个名为CoreDataTutorial的基于视图的应用程序.
2.)将核心数据框架添加到项目中.右键单击Frameworks,选择Add> Existing Frameworks ...找到CoreData.frameworks并单击add.
3.)将数据模型添加到项目中.右键单击Resources,选择Add> New File ...在iOS下选择Resource,然后选择Data Model并点击Next.
将文件命名为CoreDataTutorial.xcdatamodel,然后单击下一步.
4.)双击我们刚刚创建的文件CoreDataTutorial.xcdatamodel.这将打开模型对象编辑器.
在左上方窗格中,单击+符号以添加新实体.
通过在右上方窗格中输入名称来命名实体"SomeName".
当实体仍处于选中状态时,单击顶部中间窗格中的+符号,然后选择Add Attribute.Name this Attribute"some_attribute_name"并将其设置为String类型.
5.)现在我们将在两个实体之间建立关系.在实体窗格中选择您的实体.单击属性窗格中的+符号,然后选择"添加关系".将关系命名为"creation",将Destination设置为Release,将Delete Rule设置为Cascade.
要做反向,我们在实体窗格中选择Release.单击属性窗格中的+符号,然后选择"添加关系".将关系命名为"creator",将Destination设置为Artist,将Inverse设置为release,将Delete Rule设置为Cascade.
您现在可以关闭对象编辑器.
6.)展开其他源并双击CoreDataTutorial_Prefix.pch.为CoreData添加导入.
#ifdef __OBJC__
#import <foundation foundation.h="">
#import <uikit uikit.h="">
#import <coredata coredata.h="">
#endif
Run Code Online (Sandbox Code Playgroud)
这使我们不必将其导入每个文件.
7.)接下来,我们将设置app delegate头文件,然后设置实现文件.
首先是头文件.我们需要为NSManagedObjectContext,NSManagedObjectModel和NSPersistentStoreCoordinator创建变量.
我们还将声明一个名为applicationDocumentsDirectory的操作,此操作将获取condiments目录的路径,其中我们的数据将存储在SQLite文件中.以及在应用退出时保存上下文的操作.
这是我们完成后头文件的样子.请记住,我们将import语句添加到CoreDataTutorial_Prefix.pch文件中,因此我们不需要在此处导入它.
#import <uikit uikit.h="">
@class CoreDataTutorialViewController;
@interface CoreDataTutorialAppDelegate : NSObject <uiapplicationdelegate>
{
UIWindow *window;
CoreDataTutorialViewController *viewController;
@private
NSManagedObjectContext *managedObjectContext;
NSManagedObjectModel *managedObjectModel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet CoreDataTutorialViewController *viewController;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
@end
Run Code Online (Sandbox Code Playgroud)
8.)如果您不使用ARC,请注意取消分配内存
9.)实现applicationDocumentsDirectory方法.
/**
Returns the URL to the application's Documents directory.
*/
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Run Code Online (Sandbox Code Playgroud)
10.)接下来,实现saveContext方法:
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
Run Code Online (Sandbox Code Playgroud)
11.)最后为你的变量实现你的访问器方法,就是这样.