应用程序在CoreData PathForResource上崩溃

问题描述:

我遇到了CoreData应用程序的问题......当您选择使用核心数据存储时,它在Apple提供的boiler plate代码上崩溃。应用程序在CoreData PathForResource上崩溃

- (NSManagedObjectModel *)managedObjectModel { 

if (managedObjectModel_ != nil) { 
    return managedObjectModel_; 
} 
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"iLoveForLife" ofType:@"momd"]; 
NSURL *modelURL = [NSURL fileURLWithPath:modelPath]; 
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];  
return managedObjectModel_; 
} 

它说*终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: '* - [NSURL initFileURLWithPath:]:零字符串参数'

这里的任何想法....

不知何故,我失去了我的xcodedatamodeld文件。当我创建一个新的它叫做xcodedatamodel。

+0

我得到的是同样的东西,但我的“妈妈”引用与我的xcdatamodel相同。这应该工作吗? – quantumpotato 2011-04-25 17:49:10

+0

我所做的只是将文件从xcodedatamodel重命名为xcodedatamodeld,并且工作正常。不知道为什么。 – logixologist 2011-05-27 06:46:47

有同样的问题。 我正在通过添加一个测试来查看modelURL是否为零 - 它与所有数据模型现在在iOS5 SDK中“版本化”的事实有关。

我现在这样做:

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"iLoveForLife" 
                 ofType:@"momd"]; 
NSURL *modelURL = [NSURL fileURLWithPath:modelPath]; 
if (modelURL == nil) 
     NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"iLoveForLife" 
                   ofType:@"mom"]; 
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 

注意 “ofType” 参数从momd变化的妈妈。我知道这并不理想,但它会让你失去知觉。 只要我想通了,为什么发生这种情况,我会发布一些:-)

你应该使用下面的方法让你的管理对象模型:

managedObjectModel_ = [NSManagedObjectModel mergedModelFromBundles:nil] 

它将为所有搜索可用的主要应用程序包中的管理对象模型