【问题标题】:App crashing on CoreData PathForResource应用程序在 CoreData PathForResource 上崩溃
【发布时间】:2011-02-08 07:33:32
【问题描述】:

我遇到了一个 CoreData 应用程序的问题...当您选择使用 CORE DATA FOR STORAGE 时,它在 Apple 提供的样板代码上崩溃。

- (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_;
}

上面写着 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

这里有任何想法......

【问题讨论】:

    标签: core-data


    【解决方案1】:

    不知何故,我丢失了 xcodedatamodeld 文件。当我创建一个新的时,它被称为 xcodedatamodel。

    【讨论】:

    • 我得到了同样的东西,但我的“momd”引用与我的 xcdatamodel 相同。这应该有效吗?
    • 我所做的只是将文件从 xcodedatamodel 重命名为 xcodedatamodeld 并且它起作用了。不知道为什么。
    【解决方案2】:

    您应该使用以下方法来获取您的托管对象模型:

    managedObjectModel_ = [NSManagedObjectModel mergedModelFromBundles:nil]
    

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

    【讨论】:

      【解决方案3】:

      有同样的问题。 我正在通过添加一个测试来解决它,以查看 modelURL 是否为 nil - 这与所有数据模型现在在 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 更改为 mom。我知道这并不理想,但它会让你摆脱困境。 一旦我弄清楚为什么会发生这种情况,我会发布一些东西:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多