Coredata 的 dataModel 可以是除主包之外的任何其他包的一部分吗
我有一个数据模型文件“Abcde.xcdatamodeld”。它目前是我的主包的一部分,我使用以下代码引用它。
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Abcde" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
}
现在我有一个要求,其中“Abcde.xcdatamodeld”应该成为不同捆绑包的一部分,并且应该从新捆绑包引用它。 `
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSBundle *dataModelBundle = [NSBundle bundleWithIdentifier:@"com.product.dataModelBundle"];
NSString *modelPath = [dataModelBundle pathForResource:@"Abcde" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
}
但我在运行上述代码时遇到崩溃。我的问题是;
- 数据模型类可以是除主包之外的任何其他包的一部分。
- 如果是,那么我在这里哪里出错了。
I have a datamodel file "Abcde.xcdatamodeld" . It is currently a part of my main bundle and I am referencing it by using the following code.
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Abcde" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
}
Now I have a requirement where in the "Abcde.xcdatamodeld" should be made to be a part of a diffrent bundle and it should be referenced from the new bundle. `
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSBundle *dataModelBundle = [NSBundle bundleWithIdentifier:@"com.product.dataModelBundle"];
NSString *modelPath = [dataModelBundle pathForResource:@"Abcde" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
}
But I am experiencing a crash while running the above code.. MY questions are;
- Can a datamodel class be part of any other bundle than the main bundle.
- If yes, then where am I going wrong here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,可以,我不知道附带的捆绑包来自哪里。如果它包含在另一个项目中的 xcode 项目中,则必须确保您的包包含在主包中。
看看这个:如何将捆绑包包含在主项目 xcode 4
要快速检查,您可以使用“显示包内容”查看应用程序包,看看包含的包是否存在。
然后,从那里开始,您必须寻找包含数据模型的包。
这就是我在我的一个项目中所做的。最困难的部分是包含外部捆绑包。
我想你的代码已经差不多完成了。
我刚刚从我的代码中复制并粘贴,并使其更详细以便更好地解释。
Yes you can, I don't know where the included bundle come from. If it is included from an xcode project within another project you have to make sure that your bundle is included in the main bundle.
Have a look at this: How to include a bundle in main project xcode 4
For a quick check you can take a look at your application bundle with "Show package content" and see if the included bundle is there.
Then, starting from there you have to look for the bundle containing your data model.
This is what I did in a project of mine. The hardest part was to include the external bundle.
I think you are almost there with your code.
I have just copied and paste from my code, and make it more verbose to explain better.