iOS CGRectOffset 和核心数据问题
我知道这没有任何意义,但我在使用 Core Data 构建并调用 CGRectOffset 的 iPhone 应用程序中遇到了一个非常奇怪的错误。我的应用程序委托的 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:无法找到实体名称“TJournal”的 NSManagedObjectModel””
如果我注释掉 CGRect 行,它运行正常。 for 循环内的调用执行得很好(它将数据写入名为 TJournal 的 Core Data DB 实体,并且完全按照预期执行操作。)显然,CGRectOffset 不依赖于 Core Data,因此我猜测此错误是虚假的。但我一生都无法弄清楚。
我尝试过清理所有目标,清除模拟器中的数据库等。但似乎没有任何效果。
有什么想法吗?谢谢!
I know this doesn't make any sense, but I'm getting a really strange error in an iPhone App I am building using Core Data and calling CGRectOffset. My App Delegate's didFinishLaunchingWithOptions method looks like this:
- (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;
}
Currently, the app crashes with the following error when I leave the CGRect viewframe line in:
"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'TJournal''"
If I comment out the CGRect line, it runs fine. The call inside the for loop executes just fine (it write's data to a Core Data DB entity names TJournal, and does exactly what it's supposed.) Obviously, there is no dependence on Core Data for CGRectOffset, so I'm guessing this error is spurious. But I can't, for the life of me, figure it out.
I've tried cleaning all targets, wiping out the database in the simulator, etc. But nothing seems to work.
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,当您引用
jvc.view.frame
时,它会动态加载jvc的视图。如果视图(或 xib!)的内容在加载时依赖于托管对象上下文,则可能会产生错误。尝试将
jvc.managementObjectContext = context;
行移至JournalListVC *jvc = [[JournalListVC alloc] init];
之后的右侧。(PS:你的视图不应该考虑状态栏;相反,你的 UIWindow 应该这样做,然后你的视图控制器的视图框架应该只是窗口的边界。)
Note that when you reference
jvc.view.frame
, it is dynamically loading jvc’s view. If the contents of the view (or xib!) have a dependency on a managed object context when it’s loaded, that could produce the error.Try moving the
jvc.managedObjectContext = context;
line to right afterJournalListVC *jvc = [[JournalListVC alloc] init];
.(PS: your view shouldn’t have to account for the status bar; instead, your UIWindow should do it, and then your view controller’s view’s frame should just be the window’s bounds.)