开发者

App crashing on CoreData PathForResource

开发者 https://www.devze.com 2023-02-08 23:55 出处:网络
I have an issue with a CoreData app... it crashes on boiler plate code supplied by apple when you select USE CORE DATA FOR STORAGE.

I have an issue with a CoreData app... it crashes on boiler plate code supplied by apple when you select USE CORE DATA FOR STORAGE.

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

It says 开发者_运维百科* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

Any ideas here....


Somehow I lost my xcodedatamodeld file. When I created a new one it got called xcodedatamodel.


You should use the following to get your managed object model :

managedObjectModel_ = [NSManagedObjectModel mergedModelFromBundles:nil]

It will search for all available managed object models in the main application bundle


Had the same issue. I'm working around it by adding a test to see if modelURL is nil -Its got to do with the fact that all data models are now "versioned" in the iOS5 SDK.

I now do this:

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];

Note the "ofType" parameter changing from momd to mom. I know this isn't ideal, but it'll get you unstuck. As soon as I've figured out why this is happening, I'll post something :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消