以下是我的代码的一部分:
[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];
有人知道我是否遗漏了这里或其他任何方式来实现这一目标?感谢。
这里我也给出了我的输出:
答案 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]];
}];