這裏的問題是,您的訪問和您的伊娃具有相同的名稱。這就是underbar ivar convention的來源。在這裏,你沒有使用訪問器來訪問你的屬性,你直接使用了支持變量,所以它不會被初始化。相反,確保你總是通過你的訪問器方法,你不會有問題。因此,改寫違規方法(和任何其他人使用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
。我不喜歡這種方法,因爲人們忘記它不是一個直接的屬性訪問,而是使用訪問器,所以他們認爲可以直接訪問的點語法交換,當它不是時(比如你的情況)。
您的託管對象上下文爲零。我認爲這是一個伊娃...它沒有被設置任何東西。 –
感謝您的回覆。你能告訴我如何解決這個問題嗎? – Zack
不幸的是,或者我會把它放在答案中。您需要添加更多代碼,這裏沒有足夠的信息。在你的視圖控制器中你在哪裏設置managedObjectContext ivar? –