gpt4 book ai didi

objective-c - 没有主从模板的核心数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:04 24 4
gpt4 key购买 nike

我想创建一个使用 Core Data 的 iphone 应用程序。据我了解,只有主从应用程序模板让我可以选择使用核心数据。但它会创建 TableView 。

我想使用的是 View Controller 而不是 TableView Controller 。我无法将核心数据与单 View 应用程序模板一起使用..

我应该遵循哪种方式来克服这个问题?

谢谢。

最佳答案

您应该意识到 CoreData 是一个不绑定(bind)任何 UIKit 组件(如 UITableView)的框架。您可以在任何类型的应用程序中自由使用它。您所要做的就是创建管理 CoreData 操作的单例类,并将 CoreData.framework 添加到您的项目中。

这是我的 DataAccessLayer 模板:

数据访问层.h

#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

#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 {
@synchronized(self) {
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)
{
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"];
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:@"words_db.sqlite"];

NSError *error = nil;
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

您还必须创建一个 .xcdatamodeld 文件以创建您的数据模型对象。并将此处的名称替换为适当的

  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];

关于objective-c - 没有主从模板的核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11631379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com