Zac*_*ack 5 xcode core-data ios
我正在使用Core Data并尝试使用简单的数据模型来显示数据.该应用程序崩溃并给我这个错误消息
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称'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];
}
Run Code Online (Sandbox Code Playgroud)
您可能想将您的 ivars 更改为您不会想使用的东西,或者会立即明显看出您没有使用过访问器,例如_managedObjectContext或什至_privateContext或其他任何东西,直到您习惯访问属性为止通过访问器。如果您不喜欢使用 Objective-C 语法来访问属性,您可以使用点语法,但您必须始终记住要通过self,例如self.managedObjectContext。我不喜欢这种方法,因为人们忘记了它不是直接属性访问,而是使用访问器,因此他们认为可以将点语法交换为直接访问,但事实并非如此(就像您的情况一样)。
| 归档时间: |
|
| 查看次数: |
2854 次 |
| 最近记录: |