iOS CGRectOffset 和核心数据问题

发布于 2024-11-04 11:18:24 字数 1671 浏览 0 评论 0原文

我知道这没有任何意义,但我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2024-11-11 11:18:24

请注意,当您引用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 after JournalListVC *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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文