2013-02-21 274 views
0

我無法保存到一個變量letsMeet.startTimeLabel。然而,在選擇NSLog顯示正確的值後,我保存到另一個變量(letsMeet.endTimeLabel)後,letsMeet.startTimeLabel變爲(NULL)。下面是代碼:核心數據:未保存

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext]; 
switch (actionSheet.tag) 
{ 
    case 1: 
    { 
     if (buttonIndex == 0) 
     { 
      UIDatePicker *startDatePicker = (UIDatePicker *)[actionSheet viewWithTag:kDatePickerTag1]; 

      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
      [dateFormatter setDateFormat:@"dd"]; 
      NSDate *selectedDate = [startDatePicker date]; 

      NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init]; 
      [dayFormatter setDateFormat:@"EEEE"]; 
      NSDate *selectedDay= [startDatePicker date]; 

      NSDateFormatter *monthFormatter = [[NSDateFormatter alloc] init]; 
      [monthFormatter setDateFormat:@"MMMM"]; 
      NSDate *selectedMonth = [startDatePicker date]; 

      NSString *date = [[NSString alloc] initWithFormat:@"%@", [dateFormatter stringFromDate:selectedDate]]; 
      DateLabel.text = date; 
      [letsMeet setDateLabel:date]; 

      NSString *month = [[NSString alloc] initWithFormat:@"%@", [dayFormatter stringFromDate:selectedMonth]]; 
      MonthLabel.text = month; 
      [letsMeet setMonthLabel:month]; 

      NSString *day = [[NSString alloc] initWithFormat:@"%@", [monthFormatter stringFromDate:selectedDay]]; 
      DayLabel.text = day; 
      [letsMeet setDateLabel:day]; 

      NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; 
      [timeFormatter setDateFormat: @"h:mm a"]; 
      NSDate *selectedStartTime = [startDatePicker date]; 

      NSString *startTime = [[NSString alloc] initWithFormat:@"%@", [timeFormatter stringFromDate:selectedStartTime]]; 
      StartTimeLabel.text = startTime; 
      [letsMeet setStartTimeLabel:startTime]; 

      NSError *error = nil; 
      if (![managedObjectContext save:&error]){ 

       NSLog(@"Error Saving"); 
      } 
     } 
     NSLog (@"This is the StartTime after selecting %@", letsMeet.startTimeLabel); 
    } 
    break; 

    case 2: 
    { 
     if (buttonIndex == 0) 
     { 
      UIDatePicker *endTimePicker = (UIDatePicker *)[actionSheet viewWithTag:kDatePickerTag2]; 
      NSDateFormatter *endTimeFormatter = [[NSDateFormatter alloc] init]; 
      [endTimeFormatter setDateFormat: @"h:mm a"]; 
      NSDate *endSelectedTime = [endTimePicker date]; 
      NSString *endTime = [[NSString alloc] initWithFormat:@"%@", [endTimeFormatter stringFromDate:endSelectedTime]]; 
      EndTimeLabel.text = endTime; 
      [letsMeet setEndTimeLabel:endTime]; 
      NSLog (@"This is the EndTime %@", letsMeet.endTimeLabel); 
      NSLog (@"This is the StartTime after selecting BOTH %@", letsMeet.startTimeLabel); 
     } 
     else if (buttonIndex == 1) 
     { 
      EndTimeLabel.text = @"Whenever"; 
      [letsMeet setEndTimeLabel:EndTimeLabel.text]; 
     } 
     NSError *error = nil; 
     if (![managedObjectContext save:&error]) { 
    } 

}break; 

    // Handle the error. 
} 

} 


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    UIViewController *destinationViewController = segue.destinationViewController; 
    NSLog (@"Prepare For Segue StartTime %@", letsMeet.startTimeLabel); 
    NSLog (@"Prepare For Segue EndTime%@", letsMeet.endTimeLabel); 
} 

這裏是日誌:

2013-02-20 21:38:24.253 AppointmentTime[3129:c07] This is the StartTime after selecting 9:30 AM 
2013-02-20 21:38:32.325 AppointmentTime[3129:c07] This is the EndTime 12:15 PM 
2013-02-20 21:38:32.325 AppointmentTime[3129:c07] This is the StartTime after Selecting BOTH (null) 
2013-02-20 21:38:34.069 AppointmentTime[3129:c07] Prepare For Segue StartTime (null) 
2013-02-20 21:38:34.069 AppointmentTime[3129:c07] Prepare For Segue EndTime12:15 PM 

問:爲什麼會letsMeet.startTimeLabel顯示糾正第一次選擇結束時間之後,它改變爲NULL。請注意EndTime繼續顯示正確​​的值,直到prepareForSegue。奇怪的!

+0

因爲它們是不同的實例。 – Rog 2013-02-21 05:29:05

+0

你是什麼意思? 我也爲PrepareForSegue中的兩個變量做了NSLog,並且StartTime顯示爲NULL,而EndTime顯示正確的值。我更新了代碼和日誌。 – user1107173 2013-02-21 05:40:37

回答

1

根據您的日誌和代碼,您正在輸入切換塊兩次。這意味着您要輸入兩次actionSheet:clickedButtonAtIndex:方法。所以每次你輸入方法

letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext]; 

語句被執行兩次,依次創建兩個對象。您可以通過從商店獲取進行查看。

所以你檢查兩個不同的對象,因此null屬性。

如果您只使用一個管理對象,則可能在執行insertNewObjectForEntityForName:inManagedObjectContext:之前爲對象添加nil檢查。這將確保您使用的是同一個對象。
如果您同時使用多個對象,請使用對象ID或某個唯一鍵來標識對象並對其進行操作。

編輯:
您可以檢查與下面的代碼零:

if(letsMeet==Nil){ 
    letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext]; 
} 

這隻會工作,如果對象您所呼叫的actionSheet:clickedButtonAtIndex:方法始終是在內存中。但是,由於您堅持不懈,您可能需要從商店獲取對象,然後檢查否。的物體。

NSError *error; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:[NSEntityDescription entityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext]]; 
NSArray *objectArray = [managedObjectContext executeFetchRequest:request error:&error] 
if(objectArray.count==0){ 
    letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext]; 
}else{ 
    letsMeet = (LetsMeet *)[objectArray objectAtIndex:0]; 
} 

注:如果你需要堅持只有一對夫婦的變量,核心數據可能是矯枉過正。改爲使用NSUserDefaults,並保持簡單。

+0

謝謝你的回覆。我只使用一個managedObject。你能給我一個關於如何檢查nil的例子,以確保我使用的是同一個managedObject,並在哪裏放置它? – user1107173 2013-02-21 06:45:10

+0

只需創建一次對象。我已經更新了我的答案。 – Rakesh 2013-02-21 06:58:26

+0

我試圖檢查零,但它不起作用。我做了一個NSLog中,如果語句,它永遠不會被執行。 StartLog和EndTime的NSLog都顯示爲NULL。 然後我嘗試了您的提取代碼。同樣的事情發生。 NSLog裏面的if語句也永遠不會被執行。 不確定爲什麼它跳過if語句。我甚至嘗試在ViewDidLoad中移動這兩個代碼並得到相同的結果。我應該嘗試使用ObjectID嗎?我不知道如何獲得唯一的ObjectID。謝謝 – user1107173 2013-02-22 16:59:28