objective-c - NSEntityDescription 未找到

标签 objective-c core-data

我有一个 CoreData在根 View Controller 上工作的数据库。我有第二个 UIViewController其中当我切换到,并使用相同的行来获取 NSEntityDescription ,我收到以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: +entityForName: could not locate an NSManagedObjectModel for entity name 'Channel''



我这样做如下:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Channel" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];

错误发生在 NSEntityDescription *entity线。如果该线路适用于另一个 UIViewController,这怎么可能? ?

谢谢

最佳答案

错误提示 ManagedObjectContext有一个问题。

您是否正在通过您的 ManagedObjectContext从您的第一个 View 到第二个 View (如果不尝试)

如果这不起作用,您可以将代码发布到您分配的位置 self.context
你的意思是这样的代码在你的 appDelegate 中在 awakeFromNib方法:

RootViewController *rootViewController = (RootViewController *)[self.navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;

您需要第二行来传递实际 managedObjectContext
你可以把第二行放在你的 firstView 中像这样:
//secondViewController creation here
secondViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:secondViewController
                                         animated:YES];

当然你需要设置必要的@property .
这样做来自 UIViewControllerUIViewControllerappDelegate开始您将传递对该 NSManagedObjectContext 实例的引用.

UITabBarController Class Reference如果您需要更多信息,这里有。
只要确保在进行分配时 IBOutlet已设置,您的 NSManagedObject已经创建。

An array of the root view controllers displayed by the tab bar interface.
@property(nonatomic, copy) NSArray *viewControllers

关于objective-c - NSEntityDescription 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8131053/

相关文章:

objective-c - 自定义 NSURLProtocol 应该遵循重定向本身,还是让其客户担心?

ios - Alamofire 与 SugarRecord 冲突?

ios - 解析 XML 时字间距过大

c# - 如何在 Monotouch 中绑定(bind) NSObject<EAAccessoryDe​​legate>?

iphone - 从iphone实时发送音频到服务器?

ios - iOS核心数据“乐观锁定失败”

ios - 备份还原SQLite核心数据iphone

ios - 无限显示本地通知

ios - tableView + CoreData 上的空单元格

objective-c - 为什么 arc4random 会返回奇怪的值?