Ios &引用;无法加载NSManagedObjectModel。nil是一个非法的URL参数";
我想从AppDelegate获取托管对象上下文,但在我将两行代码放入该方法后,即使我没有做任何其他操作,应用程序也崩溃了,调试区域中有一条消息:“CoreData:无法加载NSManagedObjectModel。nil是非法的URL参数…” 在我的方法中添加的代码:Ios &引用;无法加载NSManagedObjectModel。nil是一个非法的URL参数";,ios,objective-c,iphone,core-data,Ios,Objective C,Iphone,Core Data,我想从AppDelegate获取托管对象上下文,但在我将两行代码放入该方法后,即使我没有做任何其他操作,应用程序也崩溃了,调试区域中有一条消息:“CoreData:无法加载NSManagedObjectModel。nil是非法的URL参数…” 在我的方法中添加的代码: AppDelegate *delegate = [UIApplication sharedApplication].delegate; NSManagedObjectContext *managedObjectContext =
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *managedObjectContext = delegate.managedObjectContext;
-AppDelegate中的managedObjectModel方法:
- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FoodPin" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
以及-managedObjectContext方法:
- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
“FoodPin”是我的项目名。那么这里出了什么问题?我对iPhone编程(尤其是核心数据)还不熟悉
有人能帮我吗
谢谢…问题是这一行:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FoodPin" withExtension:@"momd"];
modelURL
为nil
表示系统无法找到资源FoodPin.momd
确保项目中有一个名为
FoodPin
的核心数据模型。它将在项目导航器中显示为FoodPin.xcdatamodeld
。NSURL*modelURL=[[NSBundle mainBundle]URLForResource:@“XYZ”带扩展名:@“momd”]
确保您的数据模型名称与URLForResource:XYZ匹配。我也有同样的问题,但对我来说,模型URL设置正确。问题是我的*.xcdatamodeld文件不再位于复制包资源中。我不知道为什么它会消失,但再次添加它可以解决问题 以下是如何修复它: 您可以使用project>Build Phases>Copy Bundle Resources>“+”按钮,然后选择xcdatamodeld文件,我遇到了相同的错误
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"name" withExtension:@"momd"];
名字中的“n”必须是“n”:就像obj c中的其他所有内容一样,这位母亲是区分大小写的。好的,我必须做的就是这样。我已经将iOS版本恢复到9.x,以便在我拥有的旧版iPad上运行。这导致了错误,因为以前的版本在捆绑包中必须区分大小写。请尝试使用“mom”而不是“momd”
NSURL*modelURL=[[NSBundle mainBundle]URLForResource:@“name”带扩展名:@“mom”] 问题是这
[[NSBundle mainBundle]URLForResource:@“FoodPin”带扩展名:@“momd”]
正在modelURL
中返回nil。您的项目中有一个名为FoodPin的核心数据模型吗?哦……非常感谢,我忘了更改.xcdatamodeld文件的名称!注意:即使该文件显示扩展名为xcdatamodeld
,该方法仍应查找扩展名为momd
(与此答案中的代码相同)。即使该文件与我的项目中的文件同名,我也会收到相同的错误。@ChanchalRaj到目前为止有任何解决方案吗?这解决了我的问题。我不知道发生了什么事。查看我的提交历史记录,*。xcdatamodeld从未处于复制包资源构建阶段。我使用的是Xcode 8 beta 6。我删除了*。XCDataModeled从复制包资源构建阶段开始,问题仍然得到解决。可能是Xcode的缓存问题,这意味着执行清理或清理生成文件夹可能会为其他人解决此问题。您的建议修复了我的问题。谢谢