加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 核心数据给出错误

发布时间:2020-12-14 19:07:09 所属栏目:百科 来源:网络整理
导读:我正在使用Core Data并尝试使用简单的数据模型来显示数据.该应用程序崩溃并给我这个错误消息 Terminating app due to uncaught exception ‘NSInvalidArgumentException’,reason: ‘+entityForName: nil is not a legal NSManagedObjectContext parameter s
我正在使用Core Data并尝试使用简单的数据模型来显示数据.该应用程序崩溃并给我这个错误消息

Terminating app due to uncaught exception ‘NSInvalidArgumentException’,reason: ‘+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name ‘Remind”

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

我也提出了断点,它就在这里停下来:

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

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

解决方法

这里的问题是你的访问者和你的ivar有相同的名字.这就是underbar ivar惯例的来源.在这里,您没有使用访问者访问您的属性,您直接使用支持变量,因此它永远不会被初始化.相反,请确保您始终使用访问器方法,并且您不会遇到任何问题.因此,重写有问题的方法(以及使用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.我不喜欢这种方法,因为人们忘记了它不是直接的属性访问而且它正在使用访问器,所以他们认为可以交换点语法进行直接访问,而不是(就像你的情况一样).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读