6
A
回答
11
Xcode 4.3.2 添加核心数据框架。
选择Target-> Summary Pane-> Linked Frameworks &库。
在上述图像CoreData框架已添加。你可以点击下面的'+'按钮来添加你选择的框架。
一次U CICK ON'+'按钮U'LL看到下面的图像屏幕。
将新文件添加到它去文件 - >新建文件 - > iOS的标签 - > CoreData setion.You可以将文件乌尔选择
9
添加CoreData框架该项目,然后创建一个.xdatamodeld文件(File-> New-> CoreData-> Data Model)。将其命名为DataModel。然后创建一个单独的类将处理所有的数据持久化操作:
.H
//
// DataAccessLayer.h
//
//
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface DataAccessLayer : NSObject
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (strong, nonatomic) NSPersistentStoreCoordinator *storeCoordinator;
+ (DataAccessLayer *)sharedInstance;
- (void)saveContext;
@end
.M
//
// DataAccessLayer.m
//
//
// Created by admin on 2/27/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "DataAccessLayer.h"
@interface DataAccessLayer()
- (NSURL *)applicationDocumentsDirectory;
@end
@implementation DataAccessLayer
@synthesize storeCoordinator;
@synthesize managedObjectModel;
@synthesize managedObjectContext;
+ (DataAccessLayer *)sharedInstance {
__strong static DataAccessLayer *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[DataAccessLayer alloc] init];
sharedInstance.storeCoordinator = [sharedInstance persistentStoreCoordinator];
sharedInstance.managedObjectContext = [sharedInstance managedObjectContext];
});
return sharedInstance;
}
#pragma mark - Core Data
- (void)saveContext {
NSError *error = nil;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
NSLog(@"error: %@", error.userInfo);
/*
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]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!"
message:@"Something has gone terribly wrong! You need to reinstall the app in order for it to work properly."
delegate:nil
cancelButtonTitle:@"Close."
otherButtonTitles:nil, nil];
[alert show];
}
}
}
#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;
}
if (storeCoordinator != nil)
{
self.managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:storeCoordinator];
}
return managedObjectContext;
}
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created from the application's model.
*/
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil)
{
return managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];
self.managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
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 (storeCoordinator != nil)
{
return storeCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"model.sqlite"];
NSError *error = nil;
self.storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![storeCoordinator 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.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!"
message:@"Something has gone terribly wrong! You need to reinstall the app in order for it to work properly."
delegate:nil
cancelButtonTitle:@"Close."
otherButtonTitles:nil, nil];
[alert show];
}
return storeCoordinator;
}
#pragma mark Application's Documents directory
/**
Returns the URL to the application's Documents directory.
*/
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
@end
3
无论是hp iOS Coder的和Eugene的答案是正确的!
核心数据文件(或项目)被配置为:
- 链接,包括核心数据框架(并且这样做在你的项目的
.pch
文件导入语句) - 应用程序的委托头(.h)包括属性声明一个
context
,model
和coordinator
(如上) - 的
app delegate
的.M定义saveContext
,managedObjectContext
,managedObjectModel
,persistentStoreCoordinator
,applicationDocumentDirectory
个函数/方法 - 的数据模型,如上
相关问题
- 1. 如何将OpenCV包含到XCode 4 C++项目中?
- 2. 如何将Chipmunk库包含到iPhone Xcode项目中?
- 3. 如何将Core Animation框架添加到我的Xcode项目中?
- 4. 将iOS Storyboard项目包含到另一个项目中?
- 5. 如何在iOS上的Core Data中创建新创建的关系?
- 6. Xcode 4.2如何将一个项目包含到另一个项目中?
- 7. 如何在xcode 6中创建一个空的ios项目?
- 8. 如何在一个主项目中包含多个Xcode项目
- 9. 使用xcode和core-data中的代码创建托管对象
- 10. 在Xcode项目中包含dgles
- 11. 在iOS项目中将测量单位存储在Core Data中的好方法
- 12. 如何将Nuget包复制到DotNet Core项目中?
- 13. 如何在MFC中创建包含2个项目的菜单?
- 14. 如何将Crittercism集成到包含多个子项目的Xcode工作区中?
- 15. xcode 3.2:如何添加包含目录到我的项目?
- 16. 如何将qarray包含到QTCreator项目
- 17. 如何创建DLL其中将包含
- 18. 如何将一个Xcode项目用于iOS中的另一个Xcode项目?
- 19. 将uuid.h包含到Android NDK项目中
- 20. Xcode - 如何将c库和头文件包含到可可项目中?
- 21. 已创建iOS项目定义
- 22. 如何将Core Data添加到iOS的静态框架?
- 23. 如果indexPath是已知的,则从Core Data中删除条目
- 24. 如何使用cmake将资源包添加到xcode项目中?
- 25. 如何从Visual Studio 2017 RC中的.NET Core MSBuild项目创建NuGet包?
- 26. 如何将库包含到我的Swift项目中?
- 27. Unity-iOS:将Unity iOS项目分离到任何现有的iOS Xcode项目
- 28. 在Eclipse PDT中,如何将Subclipse项目包含到另一个项目中?
- 29. 如何在VS 2017中创建ASP.NET Core上的Angular2项目
- 30. 如何从Xcode中删除手机上Core Data中的所有条目?
你也可以改变编辑器风格,以图形视图,那么你只需按添加实体(+新增实体)的标志。同样为了为这个特定的实体生成适当的NSManagedObject子类,选择该实体,然后在XCode - > Create NSManagedObject Subclass ...中选择“Editor”选项,它将为该实体创建相应的.h&.m文件。 – raaz 2012-06-26 10:36:45
@raaz是的,我同意。 – 2012-06-26 11:03:37