2012-03-08 2 views
1

다른 답변을 많이 시도했지만이 기능을 사용할 수없는 것 같습니다. 내가 가지고있는 기존 탭 기반 프로젝트에 코어 데이터를 추가하려고합니다. 핵심 데이터 프레임 워크를 대상을 통해 추가하고 DataModel과 엔터티를 올바르게 설정했지만 액세스 할 수없는 것 같습니다. 나는 여러 가지 오류를 입수했습니다,하지만 가장 최근은 다음과 같습니다기존 Tab 기반 IOS 프로젝트에 CoreData를 추가하는 방법

*** 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 
나는 그것이 널 남아있는 파일의 URL을 만들 때

어떤 이유 AppDelegate.m ..... 내가 왜 아무 생각이 없습니다 ..

#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; 
    } 

이을에 코드를 통합하는 모든 정상적인 방법으로 제거 // 내가 네 내이 문제를 살펴 경우, coredata에 대해 별도의 프로젝트 유틸리티가 이해하면 .....

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

이 모두는 모델 URL에서 유래 된 것입니다. 데이터 모델 파일은 무엇입니까? 대/소문자 구분 등도 확인하십시오. 유일한 모델 인 경우 URL을 파생시키는 대신 mergedModelFromBundles를 고려하십시오. – jrturton

답변

관련 문제