-->

app getting crashed while creating the managedObje

2019-09-06 05:46发布

问题:

I am creating a managedObjectModel object from the following code

- (NSManagedObjectModel *)managedObjectModel {

if (managedObjectModel_ != nil) {
    return managedObjectModel_;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"DataHouse" ofType:@"momd"];

NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

return managedObjectModel_;

}

i have DataHouse.xcdatamodeld file in my xcode project. This code is working absolutely fine. Now, coming to the problem.... I have made a frawework out of my code by following this wonderful link. i have included my framework in a new project and tried running it. The app crashed on me exactly in the above code base. I know the reason for it too but unable to resolve it. The problem here is that while creating the managedObjectModel, the compiler is looking for the file called DataHouse.xcdatamodeld in the Main bundle. but this file is a part of the new framrwork and is not present in the main bundle. Now, how am i supposed to solve this problem.. Any ideas please...

回答1:

There are several other class methods on NSBundle to load a bundle instead of mainBundle. With them, you should be able to specify the correct bundle location.

Have a look here.