我尝试过很多不同的答案,但我似乎无法让它发挥作用。我正在尝试将Core Data添加到我现有的基于Tab的项目中。我通过目标添加了核心数据框架,我正确地设置了DataModel和实体,但我似乎无法访问它。我遇到了很多不同的错误,但最近的错误是:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
我使用预设的核心数据设置了一个基于实用程序的新项目,并直接复制了代码。我只是将文件URL名称更改为当前项目,但它不起作用。这是我的代码:
appDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
BOOL isNotFirstTime;
NSMutableArray *teamMembers;
NSMutableArray *projects;
NSMutableArray *tasks;
}
@property(readwrite, retain) NSMutableArray *teamMembers;
@property(readwrite, retain) NSMutableArray *projects;
@property(nonatomic, retain) NSMutableArray *tasks;
@property(nonatomic)BOOL isNotFirstTime;
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
@end
AppDelegate.m 出于某种原因,当我创建文件URL时,它仍然是空的.....我不知道为什么..
#import "AppDelegate.h"
#import "Task.h"
#import "Project.h"
#import "TeamMember.h"
#import "newTeamMemberWindow.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize tasks;
@synthesize teamMembers;
@synthesize projects;
@synthesize isNotFirstTime;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
NSLog(@"No Context on App Load");
}
newTeamMemberWindow *newTeamMemberWindowObject = [[newTeamMemberWindow alloc]init];
newTeamMemberWindowObject.managedObjectContext = context;
return YES;
}
//删除了在堆栈溢出上合并代码的所有常规方法
#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 from the application's model.
*/
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
这部分模型INL仍然是空的......
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TimeLines" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSLog(@"Created managedObjectModel with Url: %@", 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 (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TimeLines.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.
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]);
abort();
}
return __persistentStoreCoordinator;
}
#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
答案 0 :(得分:0)
如果我理解你有一个单独的coredata项目实用程序,如果是,请看看我的这个问题。这让我很久以前疯了!!!