NSDate没有时间用于For循环中的条件语句

时间:2014-12-07 16:09:01

标签: ios objective-c nsdate

以下是我的代码的一部分:

[postQuery findObjectsInBackgroundWithBlock:^(NSArray *myPosts, NSError *error)
 {
     if( !error )
     {
         NSMutableArray *resultArray = [NSMutableArray new];
         NSArray *createdAtGroup = [myPosts valueForKeyPath:@"@distinctUnionOfObjects.createdAt"];

         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
         [formatter setDateFormat:@"MM/dd/yyyy"];

         NSCalendar *calendar = [NSCalendar currentCalendar];
         NSDateComponents *comps = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit)
                                               fromDate:[NSDate date]];
         NSDate *createdAt = [calendar dateFromComponents:comps];

        for (createdAt in createdAtGroup)
        {
            NSLog(@"createdAt ------> %@", createdAt);

            NSMutableDictionary *entry = [NSMutableDictionary new];
            [entry setObject:[formatter stringFromDate:createdAt] forKey:@"createdAt"];

            NSArray *createdAtSections = [myPosts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"createdAt = %@", createdAt]];
            //NSArray *createdAtSections = [myPosts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"createdAt = %@", [formatter stringFromDate:createdAt]]];

            for (int i=0; i < createdAtSections.count; i++) {
                NSString *text = [[createdAtSections objectAtIndex:i] objectForKey:@"text"];
                [entry setObject:text forKey:@"text"];

            }
            [resultArray addObject:entry];
        }
         NSLog(@"%@", resultArray);

     }
 }
];

查看For循环中的条件语句:

for (createdAt in createdAtGroup)

我希望 createdAt 在没有时间的情况下声明为 NSDate ,以便我可以将文本分组到同一个日期。正如我搜索过的那样,似乎无法从 NSDate 中提取时间。但是,我发现这段代码是他们声称的,但它对我不起作用。

 NSCalendar *calendar = [NSCalendar currentCalendar];
         NSDateComponents *comps = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit)
                                               fromDate:[NSDate date]];
         NSDate *createdAt = [calendar dateFromComponents:comps];

有人知道我是否遗漏了这里或其他任何方式来实现这一目标?感谢。

这里我也给出了我的输出:

  • 2014-12-08 00:00:56.218 createdAt ------&gt; 2014-10-14 13:45:39 +0000
  • 2014-12-08 00:00:56.224 createdAt ------&gt; 2014-10-14 13:51:18 +0000
  • 2014-12-08 00:00:56.231 createdAt ------&gt; 2014-09-04 09:56:27 +0000
  • 2014-12-08 00:00:56.233 createdAt ------&gt; 2014-09-16 05:28:57 +0000
  • 2014-12-08 00:00:56.235 createdAt ------&gt; 2014-10-23 07:34:15 +0000
  • 2014-12-08 00:00:56.237 createdAt ------&gt; 2014-09-12 03:04:50 +0000
  • 2014-12-08 00:00:56.239 createdAt ------&gt; 2014-09-16 05:12:59 +0000
  • 2014-12-08 00:00:56.246 createdAt ------&gt; 2014-09-16 08:05:54 +0000
  • 2014-12-08 00:00:56.248 createdAt ------&gt; 2014-09-10 06:01:19 +0000
  • 2014-12-08 00:00:56.250 createdAt ------&gt; 2014-09-10 07:17:09 +0000
  • 2014-12-08 00:00:56.254(     {     createdAt =“2014/10/14”;     text =“Www.thecubeinn.com.tw”; },     {     createdAt =“2014/10/14”;     text =“Www.thecubeinn.com.tw”; },     {     createdAt =“09/04/2014”;     text =“梅花湖”; },     {     createdAt =“09/16/2014”;     text = Nanjuang; },     {     createdAt =“10/23/2014”;     text =“家乐福,内湖”; },     {     createdAt =“09/12/2014”;     text =“Yuangi brunch”; },     {     createdAt =“09/16/2014”;     text =“这里的食物非常好吃”; },     {     createdAt =“09/16/2014”;     text =“Nanliao harbour,hsinju”; },     {     createdAt =“09/10/2014”;     text =“Yana的家”; },     {     createdAt =“09/10/2014”;     text =“王龙培”; } )

1 个答案:

答案 0 :(得分:0)

好的,所以你要做的就是遍历所有项目,并将它们放入标记为天数的桶中(NSDictionary在这里工作得很好 - NSNumber甚至可以作为密钥进行哈希处理)。

// Get the calendar for the user's locale
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     [formatter setDateFormat:@"MM/dd/yyyy"];

// Sparse dictionary, containing keys for "days with posts"
NSMutableDictionary *daysWithPosts = [NSMutableDictionary dictionary];

[myPosts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    // use this if 'createdAt' is a UTC time-stamp
    // NSDate *date = [NSDate dateWithTimeIntervalSince1970:[obj createdAt].floatValue];
    // NSNumber *uniqueDay = @([calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitEra forDate:date]); // This is not fast

    NSString *dateString = [formatter stringFromDate:[obj createdAt]]; // EDIT 1

    // Check to see if we have a day already.
    NSMutableArray *posts = [daysWithPosts objectForKey: dateString /*uniqueDay*/];

    // If not, create it
    if (posts == nil || (id)posts == [NSNull null])
    {
        posts = [NSMutableArray arrayWithCapacity:1];
        [daysWithPosts setObject:posts forKey: dateString /*uniqueDay*/];
    }

    // add post to day
    [posts addObject:obj];
}];

// Sections array for TableView
NSArray *sectionTitles = [daysWithPosts allKeys]; // keep this around to re-format to strings?
NSArray *sections = [daysWithPosts allValues];

结果是一系列可以在Tableview中使用的部分和标题。

id rowData = sections[indexPath.section][indexPath.row];

您可能还希望根据个人时间对各个日期进行排序。

[sections enumeratObjectsUsingBlock:^(id obj, NSUinteger idx, BOOL *stop) {
    [(NSMutableArray*)obj sortUsingDescriptors:@[NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:YES]];
}];