gpt4 book ai didi

ios - 核心数据 : Multiple xcdatamodeld files in the same iOS project?

转载 作者:可可西里 更新时间:2023-11-01 06:05:08 25 4
gpt4 key购买 nike

是否可以在同一项目中提供两个 xcdatamodeld 核心数据并根据条件分别加载?

我有 BTPModel.xcdatamodeldFTModel.xcdatamodeld

根据评论,下面这一行是这样做的:

NSManagedObjectModel model = [NSManagedObjectModel mergedModelFromBundles:nil];

// looks up all models in the specified bundles and merges them; if nil is specified as argument, uses the main bundle

我可以做这样的事情吗? (伪代码)

if (config == @"FT") {
model = [NSManagedObjectModel load:@"FTModel.xcdatamodeld"];
} else {
model = [NSManagedObjectModel load:@"BPTModel.xcdatamodeld"];
}

更新:

我已经试过了

        NSURL *url = [[NSBundle mainBundle] URLForResource:@"F11iModel" withExtension:@"xcdatamodeld"];

没有任何运气。 url 保持为空。

更新分机是妈妈。现在可以了!

        NSURL *url = [[NSBundle mainBundle] URLForResource:@"F11iModel" withExtension:@"momd"];

最佳答案

当然可以。需要注意的几点:

特定模型的加载不是通过某些load 方法完成的,而是通过initWithContentsOfURL 完成的。您将获得包含主包的 URLForResource 的 URL。你可以根据配置信息给这个传递不同的资源名称。

我建议您还使用不同的持久性商店,以确保不会尝试打开具有错误模型的商店(这会使您的应用程序崩溃)。

关于ios - 核心数据 : Multiple xcdatamodeld files in the same iOS project?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32671632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com