我仍然在學習如何在iOS中編程。在我承擔的項目中,我試圖更新核心數據中的信息,但我遇到了障礙。我無法更新日期和布爾屬性。請幫忙!以下是我被困住的那部分程序。遵循NSLog的輸出。核心數據日期和布爾屬性無法更新
ItemUpdateViewController *updateView = segue.sourceViewController;
NSDateFormatter* formattedDate = [[NSDateFormatter alloc] init];
formattedDate.dateFormat = @"dd/MM/yyyy";
// Update lastDone1 date
[self.monitorItem setValue:
[formattedDate dateFromString:updateView.lastDone1TextField.text]
forKey:@"lastDone1"];
NSLog(@"lastDone1 text: %@", updateView.lastDone1TextField.text);
NSLog(@"lastDone1 from updateView: %@",
[formattedDate dateFromString:updateView.lastDone1TextField.text]);
NSLog(@"lastDone1 in self.monitorItem: %@",[self.monitorItem valueForKey:@"lastDone1"]);
// Update fixed boolean value
[self.monitorItem setValue:[NSNumber numberWithBool:YES] forKey:@"fixed"];
NSLog(@"fixed: %@",[self.monitorItem valueForKey:@"fixed"]);
輸出: 2014年5月2日17:17:48.334 SGRecencyMonitor [805:70B] lastDone1文本:2014年2月5日2014年5月2日 17:17:48.336 SGRecencyMonitor [805:70b的] lastDone1 from updateView:2014-05-01 16:00:00 +0000 2014-05-02 17:17:48.336 SGRecencyMonitor [805:70b] lastDone1 in self.monitorItem:(null)
2014-05 -02 17:17:48.336 SGRecencyMonitor [805:70b] fixed:(null)
如圖所示,核心數據返回兩個(null)值。
更新:
感謝nmh!我添加了以下內容:
SGRMAppDelegate *appDelegate = (SGRMAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate saveContext];
但是結果仍然相同。它都返回(空)值。
也許self.monitorItem是零!請仔細檢查這個變量 – nmh
謝謝nmh。是的self.monitorItem爲空。由於缺乏理解,設計中有一個基礎。管理做一個重大的重新設計,現在可以。 – Sharkfin