首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从数组中获取同一天的所有日期

从数组中获取同一天的所有日期
EN

Stack Overflow用户
提问于 2013-05-11 21:27:15
回答 1查看 450关注 0票数 1

假设我有不同日期的NSMutableArray。日期以UITableView表示。如果我单击UITableViewCell,如何从NSMutableArray获取同一天的所有日期?

假设我在周一插入了3个日期,周二插入了1个日期,周三插入了4个日期,依此类推。如果我按下代表星期一的1/3日期,我如何才能获得所有3个星期一的日期而忽略其他日期?

如果可能,我如何对它们进行排序??因此,如果我在8,9,10小时(都在周一)中保存,并按下任何小时(也包括分钟),它们将被排序为8-9-10,就像它们实际发生的那样……

Tnx。

EN

回答 1

Stack Overflow用户

发布于 2013-05-11 21:47:21

NSDate和company的日期计算有点复杂,但一旦你掌握了它的诀窍,实际上是相当强大的。以下是过滤数组的一种方法:

代码语言:javascript
复制
int main (int argc, const char * argv[])
{
    @autoreleasepool {

        NSCalendar  *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSMutableArray *dates = [NSMutableArray array];
        NSDate  *selectedDate = [NSDate date];

        for (int i=0;   i<70;  i++)
            [dates addObject:[NSDate dateWithTimeIntervalSinceNow:i * 60*60*24]];



        NSInteger selectedDayOfWeek = [calendar components:NSWeekdayCalendarUnit fromDate:selectedDate].weekday;

        NSPredicate *filter = [NSPredicate predicateWithBlock:^(id object, NSDictionary *bindings) {
            return (BOOL)([calendar components:NSWeekdayCalendarUnit fromDate:object].weekday == selectedDayOfWeek);
        }];

        NSArray *filteredDates = [dates filteredArrayUsingPredicate:filter];

        NSArray *sortedDates = [filteredArray sortedArrayUsingSelector:@selector(compare:)];

        NSLog(@"%@", filteredDates);
    }
    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16497596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档