2014-01-18 45 views
1

我使用transient attribute來分發core data objectstable view sectionstransient attribute被稱爲sectionIdentifier它是根據名爲todoDueDate的屬性定義的。 我需要的方式來篩選對象都必須包含在以下組的一個只有在一個NSDate篩選案例

1. sectionIdentier = 0, **OVERDUE** , todoDueDate < today 
2. sectionIdentier = 1, **TODAY** , todoDueDate = today 
3. sectionIdentier = 2, **TOMORROW** , todoDueDate >today AND todoDueDate= = tomorrow 
4. sectionIdentier = 3, **UPCOMING** , todoDueDate > today AND todoDueDate!=tomorrow 

隨着我當前的代碼,我能夠過濾對象爲案例1,2和4(我必須在這裏放棄明天的對象)。我已經嘗試了幾種方法來過濾案例3的對象,但沒有成功。 我懇請您幫我過濾的情況下,3 這裏是我當前的代碼:

-(NSString *)sectionIdentifier{ 

    [self willAccessValueForKey:@"sectionIdentifier"]; 
    NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"]; 
    [self didAccessValueForKey:@"sectionIdentifier"]; 

    if (!tmp){ 

     NSDate *date = self.todoDueDate; 
     NSDate *todayDate = [NSDate date]; 


     NSLog(@"date= %@",date); 
     NSLog(@"todayDate = %@",todayDate); 


     NSCalendar *calendar = [NSCalendar currentCalendar]; 
     NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit); 
     NSDateComponents *date1Components = [calendar components:comps fromDate:date]; 
     NSDateComponents *date2Components = [calendar components:comps fromDate:todayDate]; 

     date = [calendar dateFromComponents:date1Components]; 
     todayDate = [calendar dateFromComponents:date2Components]; 


     if([date 
      compare:todayDate] == NSOrderedAscending) { 
      tmp = @"0";//OVERDUE 
     } 
     if([date 
      compare:todayDate] == NSOrderedDescending) { 
      tmp = @"3";//UPCOMING BUT NOT TOMORROW(PENDING THIS PART) 
     } 
     if ([date 
      compare:todayDate] == NSOrderedSame) { 
      tmp = @"1";//TODAY 
     } 


    //TOMORROW COMES HERE 



     NSLog(@"Tmp= %@",tmp); 

     [self setPrimitiveValue:tmp forKey:@"sectionIdentifier"]; 

    } 
    return tmp; 

} 

這是我更新的代碼:

-(NSString *)sectionIdentifier{ 

    [self willAccessValueForKey:@"sectionIdentifier"]; 
    NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"]; 
    [self didAccessValueForKey:@"sectionIdentifier"]; 

    if (!tmp){ 
     NSDate *today = [NSDate date]; 
     NSDate *date = self.todoDueDate; 
     NSCalendar *calendar; 
     NSInteger daysAfterToday = [calendar components:NSDayCalendarUnit 
               fromDate:today toDate:date options:0].day; 
     // NSString *section; 
     if (daysAfterToday < 0) { 
      tmp = @"0"; 
     } else if (daysAfterToday == 0) { 
      tmp = @"1"; 
     } else if (daysAfterToday == 1) { 
      tmp = @"2"; 
     } else { 
      tmp = @"3"; 
     } 


     NSLog(@"TODAY = %@", today); 
     NSLog(@"DATE = %@", date); 
     NSLog(@"DAYS AFTER TODAY = %ld",(long)daysAfterToday); 

     NSLog(@"Tmp= %@",tmp); 

     [self setPrimitiveValue:tmp forKey:@"sectionIdentifier"]; 

    } 
    return tmp; 

} 

這裏記錄的結果:

2014-01-17 22:35:53.576 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000 
2014-01-17 22:35:53.619 To-Do Pro Light[4124:a0b] DATE = 2014-01-13 04:08:32 +0000 
2014-01-17 22:35:53.656 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0 
2014-01-17 22:35:53.664 To-Do Pro Light[4124:a0b] Tmp= 1 
2014-01-17 22:35:53.739 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000 
2014-01-17 22:35:53.743 To-Do Pro Light[4124:a0b] DATE = 2014-01-18 04:13:39 +0000 
2014-01-17 22:35:53.748 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0 
2014-01-17 22:35:53.752 To-Do Pro Light[4124:a0b] Tmp= 1 
2014-01-17 22:35:53.757 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000 
2014-01-17 22:35:53.762 To-Do Pro Light[4124:a0b] DATE = 2014-01-19 04:16:14 +0000 
2014-01-17 22:35:53.766 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0 
2014-01-17 22:35:53.770 To-Do Pro Light[4124:a0b] Tmp= 1 
2014-01-17 22:35:53.776 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000 
2014-01-17 22:35:53.779 To-Do Pro Light[4124:a0b] DATE = 2014-01-25 04:11:34 +0000 
2014-01-17 22:35:53.825 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0 
2014-01-17 22:35:53.831 To-Do Pro Light[4124:a0b] Tmp= 1 

回答

1

只求日曆計算從今天到截止日期的天數,並檢查該號碼:

 NSInteger daysAfterToday = [calendar components:NSDayCalendarUnit 
      fromDate:today toDate:date options:0].day; 
     NSString *section; 
     if (daysAfterToday < 0) { 
      section = @"0"; 
     } else if (daysAfterToday == 0) { 
      section = @"1"; 
     } else if (daysAfterToday == 1) { 
      section = @"2"; 
     } else { 
      section = @"3"; 
     } 
+0

我也會嘗試你的建議,實現起來要容易得多,只需要12條代碼。非常聰明。 – mvasco

+0

+1確實非常巧妙 –

+0

我已經包含了應該在四個可能組中排序的對象,但啓動應用程序後,所有對象都包含在組1中...... – mvasco

1

稍微改寫一下你的代碼,你可以使用消息dateOnly只返回日期,時間部分(把所有的代碼在NSDate類):

-(NSDate *)dateOnly { 
    unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; 
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *components = [calendar components:flags fromDate:self]; 
    return [calendar dateFromComponents:components]; 
} 

這今天將返回:

+(NSDate *)today { 
    return [[NSDate now] dateOnly]; 
} 

然後,給出這樣的:

-(NSDate *)addDays:(NSInteger)days { 
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; 
    [offsetComponents setDay:days]; 
    return [[NSDate calendar] dateByAddingComponents:offsetComponents toDate:self options:0]; 
} 

你可以有明天:

+(NSDate *)tomorrow { 
    return [[NSDate today] addDays:1]; 
} 

由此,您可以使用[NSDate today][NSDate tomorrow]來簡化您的代碼。

此外,請記住從todoDueDate剝離時組件:

NSDate *date = [self.todoDueDate dateOnly]; 
NSDate *todayDate = [NSDate today]; 
NSDate *tomorrowDate = [NSDate tomorrow]; 

你的邏輯可以簡化爲:

  1. sectionIdentier = 0,OVERDUE,todoDueDate <今天
  2. sectionIdentier = 1,今日,todoDueDate =今天
  3. sectionIdentier = 2,TOMORROW,明天todoDueDate =
  4. sectionIdentier = 3,UPCOMING,todoDueDate>明天

在代碼:

if ([date compare:todayDate] == NSOrderedAscending) { 
    tmp = @"0";//OVERDUE 
} 
else if ([date isEqualToDate:todayDate]) { 
    tmp = @"1";//TODAY 
} 
else if ([date isEqualToDate:tomorrowDate]) { 
    tmp = @"2";//TOMORROW 
} 
else if ([date compare:tomorrowDate] == NSOrderedDescending) { 
    tmp = @"3";//UPCOMING 
} 
+0

謝謝你,我會嘗試你的建議,但是我需要你的幫助來區分明天和明天,而不是明天。 – mvasco

+0

在那裏添加了新的邏輯... –

+0

謝謝,我沒有想過這個智能解決方案.... – mvasco