iOS CGRectOffset和核心数据问题

时间:2011-04-29 04:17:02

标签: objective-c cocoa-touch ios core-data cgrect

我知道这没有任何意义,但是我在使用Core Data构建并调用CGRectOffset的iPhone应用程序中遇到了一个非常奇怪的错误。 My 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 行时,应用程序崩溃并出现以下错误:

  

“因未捕获的异常而终止应用程序'NSInternalInconsistencyException',原因:'+ entityForName:无法找到实体名称的'NSManagedObjectModel''TJournal''”

如果我注释掉CGRect行,它运行正常。 for循环内部的调用执行得很好(它将数据写入Core Data DB实体名称TJournal,并完全按照预期进行。)显然,CGRectOffset没有依赖Core Data,所以我猜这个错误是伪。但是,对于我的生活,我不能想出来。

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

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

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

尝试在jvc.managedObjectContext = context;之后将JournalListVC *jvc = [[JournalListVC alloc] init];行移至右侧。

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