Cannot create an NSPersistentStoreCoordinator with a nil model

前端 未结 27 1290
囚心锁ツ
囚心锁ツ 2021-01-30 03:33

Been having my first crack at Core Data and I\'m getting the following error when running my code on my device, but it works fine on the simulator..

*

27条回答
  •  無奈伤痛
    2021-01-30 04:12

    I've experienced a weird issue with Xcode 4.3.2 and iOS 5.

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

    returns a valid URL but

    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    

    returns a null NSManagedObjectModel. But after checking the documentation, it seems like NSManagedObjectModel needs a file where as NAME_OF_THE_MODEL.momd is a directory which contains a NAME_OF_THE_MODEL.mom file. Changing the URL to

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NAME_OF_THE_MODEL" withExtension:@"mom" subdirectory:@"NAME_OF_THE_MODEL.momd"];
    

    then works. Seems weird though that Xcode generates code that doesn't work with itself...

提交回复
热议问题