我有一个 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
.这样做来自
UIViewController
至 UIViewController
从appDelegate
开始您将传递对该 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/