我知道这没有任何意义,但我在使用核心数据构建并调用 CGRectOffset 的 iPhone 应用程序中遇到了一个非常奇怪的错误。我的 App Delegate 的 didFinishLaunchingWithOptions 方法如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Setup the Managed Object Context
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
// Do something - Like exit
}
//Load up the database form a pList
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"tJournals" ofType:@"plist"];
NSMutableArray *plistJournals = [NSMutableArray arrayWithContentsOfFile:plistPath];
//Create a bunch of journals
for (NSDictionary *journal in plistJournals) {
[TJournal journalWithDictionary:journal inManagedObjectContext:context];
}
NSError *error = nil;
[context save:&error];
// ------ Create the View Controller ------
// The Scrolling List
JournalListVC *jvc = [[JournalListVC alloc] init];
// Adjust for the Status Bar's height
CGRect viewFrame = CGRectOffset(jvc.view.frame, 0.0, 20.0);
jvc.view.frame = viewFrame;
jvc.managedObjectContext = context;
// Add the View Controller to the screen
[self.window addSubview:jvc.view];
[self.window makeKeyAndVisible];
return YES;
}
目前,当我离开 CGRect viewframe 行时,应用程序崩溃并出现以下错误:
"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'TJournal''"
如果我注释掉 CGRect 行,它运行正常。 for 循环内的调用执行得很好(它将数据写入一个名为 TJournal 的 Core Data DB 实体,并且完全按照预期进行。)显然,CGRectOffset 不依赖于 Core Data,所以我猜这个错误是虚假的。但我终究无法弄明白。
我已经尝试清理所有目标,清除模拟器中的数据库等。但似乎没有任何效果。
有什么想法吗?谢谢!
最佳答案
请注意,当您引用 jvc.view.frame
时,它正在动态加载 jvc 的 View 。如果 View (或 xib!)的内容在加载时依赖于托管对象上下文,则可能会产生错误。
尝试将 jvc.managedObjectContext = context;
行移动到 JournalListVC *jvc = [[JournalListVC alloc] init];
之后。
(PS:你的 View 不应该考虑状态栏;相反,你的 UIWindow 应该这样做,然后你的 View Controller 的 View 的框架应该只是窗口的边界。)
关于objective-c - iOS CGRectOffset 和核心数据问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5827796/