ios – 核心数据给出错误

ios – 核心数据给出错误,第1张

概述我正在使用Core Data并尝试使用简单的数据模型来显示数据.该应用程序崩溃并给我这个错误消息 Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘+entityForName: nil is not a legal NSManagedObjectContext parameter se 我正在使用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.我不喜欢这种方法,因为人们忘记了它不是直接的属性访问而且它正在使用访问器,所以他们认为可以交换点语法进行直接访问,而不是(就像你的情况一样).

总结

以上是内存溢出为你收集整理的ios – 核心数据给出错误全部内容,希望文章能够帮你解决ios – 核心数据给出错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1042102.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存