1

我知道这没有任何意义,但是我在使用 Core Data 构建并调用 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 视图框线留在其中时,应用程序崩溃并出现以下错误:

“由于未捕获的异常‘NSInternalInconsistencyException’而终止应用程序,原因:‘+entityForName:找不到实体名称‘TJournal’的 NSManagedObjectModel”

如果我注释掉 CGRect 行,它运行良好。for循环内的调用执行得很好(它将数据写入Core Data DB实体名称TJournal,并且完全按照预期执行。)显然,CGRectOffset不依赖Core Data,所以我猜这个错误是虚假的。但我一辈子都想不通。

我已经尝试清理所有目标,清除模拟器中的数据库等。但似乎没有任何效果。

有任何想法吗?谢谢!

4

1 回答 1

0

请注意,当您引用时jvc.view.frame,它会动态加载 jvc 的视图。如果视图(或 xib!)的内容在加载时依赖于托管对象上下文,则可能会产生错误。

尝试将jvc.managedObjectContext = context;线移到 之后JournalListVC *jvc = [[JournalListVC alloc] init];

(PS:您的视图不应该考虑状态栏;相反,您的 UIWindow 应该这样做,然后您的视图控制器的视图框架应该只是窗口的边界。)

于 2011-05-04T19:27:05.447 回答