开发者

Problems With Core Data App-

开发者 https://www.devze.com 2023-04-12 00:00 出处:网络
When I try to build my Core Data App, which most code is coming from CoreDataBooks example, I get the error

When I try to build my Core Data App, which most code is coming from CoreDataBooks example, I get the error

'+entityForName: could not locate an NSManagedObje开发者_开发百科ctModel for entity name 'Child''

So, after reading, I found the possible causes(See This Older Stack Overflow Question):

  • Nil managed object context object.
  • Failure to add the model containing the entity to the persistent store the context uses.
  • Failure to add the correct persistent store to the context itself.

My question is, how can I tell which of these causes the error, and how can I fix it. I have little experience in debugging and core data, so this is all new.


Most likely, it is the managedObjectContext coming up nil. The way I go about solving this is either to pass the managedObjectContext to the view controller in question from the previous (if it is navigation-based) or to declare the context explicitly in the viewDidLoad like follows:

- (void)viewDidLoad {
    if (managedObjectContext == nil) {
        managedObjectContext = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    }
}

This will get the context created in your app delegate file and apply it to the view in which you are using it. There are many ways to go about assigning the context, but this is probably the most direct

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号