首页 文章

核心数据给出错误

提问于
浏览
5

我正在使用Core Data并尝试使用简单的数据模型来显示数据 . 该应用程序崩溃并给我这个错误消息

由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称'Remind''

我不完全确定,但我怎么说它是说它找不到我叫做Remind的实体?但是,我确实有一个名为Remind的实体 .

我也提出了断点,它就在这里停止:
enter image description here

任何帮助将不胜感激 . 完全处于死胡同 .

App Delegate .m中的托管上下文代码

enter image description here

1 回答

  • 1

    这里的问题是你的访问者和你的ivar有相同的名字 . 那个's where the underbar ivar convention comes from. Here, you'没有使用访问者访问你的属性,你有问题 . 因此,重写有问题的方法(以及使用 managedContextObject 属性的任何其他方法,如下所示:

    - (void)viewWillAppear:(BOOL)animated
    {
      [super viewWillAppear:animated]; // it's good practice to call the super methods, even if you're fairly certain they do nothing
    
      // Get a reference to the managed object context *through* the accessor
      NSManagedObjectContext* context = [self managedObjectContext];
    
      // From now on, we only use this reference in this method
      NSFetchRequest = [[NSFetchRequest alloc] init];
      NSEntityDescription* entity = [NSEntityDescription entityForName:@"Remind" inManagedObjectContext:context]; // <- use the local reference we got through the accessor
      [request setEntity:entity];
      NSError* error = nil;
      NSArray* array = [context executeFetchRequest:request error:&error];
      if( !array ) {
        // Do something with the error
        NSLog(@"Error Fetching: %@", error);
      }
      [self setDesitnationsArray:[array mutableCopy]];
      [destinationsTableView reloadData];
    }
    

    您可能希望将您的ivars更改为您通过访问者获得的内容,例如 _managedObjectContext 甚至 _privateContext ,或者在您习惯通过访问者访问属性之前会发现任何内容 . 如果您不喜欢用于访问属性的Objective-C语法,则可以使用点语法,但必须始终记住通过 self ,例如 self.managedObjectContext . 我不是't like this method as people forget that it'不是直接属性访问而且它正在使用访问者,所以他们认为它不是's okay to interchange the dot syntax for a direct access, when it'(就像你的情况一样) .

相关问题