2012-03-08 14 views
1

私は多くの異なる答えを試しましたが、これを動作させることはできません。私は既存のタブベースプロジェクトにコアデータを追加しようとしています。私はターゲットを介してコアデータフレームワークを追加し、データモデルとエンティティを正しく設定しましたが、アクセスできないようです。私は多くの異なったエラーを得ているが、最近のは次のとおりです。既存のTabベースのIOSプロジェクトにCoreDataを追加する方法

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model' 

私はプリセットコアデータを使用して新しいユーティリティベースのプロジェクトを設定し、として直接コードをコピーしました。私は単純にファイルURLの名前を現在のプロジェクトの名前に変更しましたが、動作しません。ここに私のコードは次のとおりです。私は、ファイルの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 何らかの理由で、それは私がなぜ...

見当がつかない..... nullのまま
#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のための独立したプロジェクトの有用性を持って理解している場合一部modelURLは.....

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

答えて