objective-c - iOS CGRectOffset 和核心数据问题

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

我知道这没有任何意义,但我在使用核心数据构建并调用 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/

相关文章:

ios - objective-c : how to execute a function when app is in the background/killed automatically based on time Intervals

ios - 根据单击的按钮更改内容?

ios - 错误 : undefined is not an object (evaluating \'RCTWebSocketManager.connect\' )

ios - NSSplitView 建议的分割分隔线没有放置在正确的位置?

ios - UIView 的程序化 "fuzzy"样式背景

ios - 我正在使用 tableview 从 url 加载图像,但 tableview 中的图像重复问题

ios - 辅助 NSThread 中的 NSTimer 不起作用

objective-c - Xcode单一窗口,显示来自xib文件的自定义 View

iphone - iPhone 的弹性橡皮筋效果?

php - AFNetworking上传图像间歇性地工作