这里的问题是您的访问器和您的 ivar 具有相同的名称。这就是下划线 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
. 我不喜欢这种方法,因为人们忘记了它不是直接的属性访问并且它正在使用访问器,所以他们认为可以将点语法交换为直接访问,而不是(就像你的情况一样)。