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. 如何將Chipmunk庫包含到iPhone Xcode項目中?
- 2. 如何將OpenCV包含到XCode 4 C++項目中?
- 3. 將XCode項目產品包含到另一個項目中
- 4. 如何將Core Animation框架添加到我的Xcode項目中?
- 5. 包含已創建的文件夾在項目中
- 6. 如何在iOS上的Core Data中創建新創建的關係?
- 7. Xcode 4.2如何將一個項目包含到另一個項目中?
- 8. 將iOS Storyboard項目包含到另一個項目中?
- 9. 如何構建+將FFMPEG包含到現有的Android項目中
- 10. 如何在xcode 6中創建一個空的ios項目?
- 11. 如何搜索已包含在xcode項目中的.txt文件中的單詞
- 12. 如何在一個主項目中包含多個Xcode項目
- 13. 使用xcode和core-data中的代碼創建託管對象
- 14. 如何將Scintilla包含在XCode 3.2.5項目中?
- 15. 在Xcode項目中包含dgles
- 16. 如何將Nuget包複製到DotNet Core項目中?
- 17. 項目的plist中包含外部創建的XML文件(在xcode中)?
- 18. 在iOS項目中將測量單位存儲在Core Data中的好方法
- 19. 如何將現有的Core Data xcdatamodeld和sqlite移動到新項目中?
- 20. 如何在項目中包含動態創建的文件?
- 21. 在Xcode 4.2.1項目中包含標題
- 22. 如何將Crittercism集成到包含多個子項目的Xcode工作區中?
- 23. xcode 3.2:如何添加包含目錄到我的項目?
- 24. 如何將qarray包含到QTCreator項目
- 25. 如何創建DLL其中將包含
- 26. Sitecore創建包含字段[]的項目
- 27. 如何在eclipse中將Maven構建的庫包含到您的項目中?
- 28. 如何將一個Xcode項目用於iOS中的另一個Xcode項目?
- 29. 將uuid.h包含到Android NDK項目中
- 30. 無法將float_pkg包含到項目中
你也可以改變編輯器風格,以圖形視圖,那麼你只需按添加實體(+新增實體)的標誌。同樣爲了爲這個特定的實體生成適當的NSManagedObject子類,選擇該實體,然後在XCode - > Create NSManagedObject Subclass ...中選擇「Editor」選項,它將爲該實體創建相應的.h&.m文件。 – raaz 2012-06-26 10:36:45
@raaz是的,我同意。 – 2012-06-26 11:03:37