gpt4 book ai didi

ios - NSDate 过滤案例

转载 作者:行者123 更新时间:2023-11-29 10:49:10 24 4
gpt4 key购买 nike

我正在使用 transient 属性 TableView 部分中分发核心数据对象 transient 属性 称为sectionIdentifier,它是根据名为todoDueDate 的属性定义的。我需要以必须包含以下组之一的方式过滤对象,并且只包含一个:

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

最佳答案

只需让日历计算从今天到截止日期的天数,并检查该数字:

        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";
}

关于ios - NSDate 过滤案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21200178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com