使用 Core Data 获取会生成“无法识别的选择器错误”?
我正在尝试检索使用核心数据保存的对象列表。创建项目时 Xcode 的默认设置没有发生任何更改。实际数据存储中存在项目,并且实体 Transaction
在保存时工作正常,但在运行以下代码时:
NSManagedObjectContext * context = [[NSApp delegate] managedObjectModel];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription
entityForName:@"Transaction"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError * error = nil;
NSArray * transactionList = [context executeFetchRequest:fetchRequest error:&error];
if (&error != nil) {
[Utility showMessage:error.description asError:YES];
} else {
[Utility showMessage:[NSString stringWithFormat: @"Items: %@", transactionList.count] asError:NO];
}
尝试创建 entity
对象时收到以下错误。
[NSManagedObjectModel permanentStoreCoordinator]:发送到实例的无法识别的选择器
我缺少什么,或者我该怎么做才能检查导致错误的原因?
脚注
- Utility
是一个静态类,它只是生成一个 NSAlert
框。
- 我一直在使用这个教程来尝试理解如何代码有效
I'm trying to retrieve a list of objects saved using Core Data. No changes where made to the default setup made by Xcode when creating the project. There are items in the actual data store, and the entity Transaction
works fine when saving but when running the following code:
NSManagedObjectContext * context = [[NSApp delegate] managedObjectModel];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription
entityForName:@"Transaction"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError * error = nil;
NSArray * transactionList = [context executeFetchRequest:fetchRequest error:&error];
if (&error != nil) {
[Utility showMessage:error.description asError:YES];
} else {
[Utility showMessage:[NSString stringWithFormat: @"Items: %@", transactionList.count] asError:NO];
}
I receive the following error when trying to create the entity
object.
[NSManagedObjectModel persistentStoreCoordinator]: unrecognized selector sent to instance
What am I missing, or what do I do to check what is causing the error?
Footnotes
- Utility
is a static class which simply generates a NSAlert
box.
- I've been using this tutorial to try and understand how the code works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一行中,您从应用委托中获取
managedObjectModel
并将其分配给NSManagedObjectContext
。您应该改为获取managedObjectContext
。In the first line you're fetching the
managedObjectModel
from your app delegate and assigning it to anNSManagedObjectContext
. You should fetch themanagedObjectContext
instead.从您的代码中,尚不清楚您到底要分配给托管对象上下文的内容。它应该是托管对象上下文,而不是托管对象模型。
另外,您应该检查
(error!=nil)
是否不是&error
。阅读 C 指针语法 (;-)。From your code, it is not clear what exactly you are assigning to managed object context. It should be a managed object context, not a managed object model.
Also, you should check if
(error!=nil)
not&error
. Read up on your C pointer syntax (;-).