【发布时间】:2014-12-07 16:09:00
【问题描述】:
下面是我的部分代码:
[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,以便我可以将 text 分组到同一日期。正如我所搜索的,似乎没有办法从 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 ------> 2014-10-14 13:45:39 +0000
- 2014-12-08 00:00:56.224 createdAt ------> 2014-10-14 13:51:18 +0000
- 2014-12-08 00:00:56.231 createdAt ------> 2014-09-04 09:56:27 +0000
- 2014-12-08 00:00:56.233 createdAt ------> 2014-09-16 05:28:57 +0000
- 2014-12-08 00:00:56.235 createdAt ------> 2014-10-23 07:34:15 +0000
- 2014-12-08 00:00:56.237 createdAt ------> 2014-09-12 03:04:50 +0000
- 2014-12-08 00:00:56.239 createdAt ------> 2014-09-16 05:12:59 +0000
- 2014-12-08 00:00:56.246 createdAt ------> 2014-09-16 08:05:54 +0000
- 2014-12-08 00:00:56.248 createdAt ------> 2014-09-10 06:01:19 +0000
- 2014-12-08 00:00:56.250 createdAt ------> 2014-09-10 07:17:09 +0000
- 2014-12-08 00:00:56.254 ( { createdAt = "10/14/2014"; text = "www.thecubeinn.com.tw"; }, { createdAt = "10/14/2014"; text = "www.thecubeinn.com.tw"; }, { createdAt = "09/04/2014"; text = "梅花湖"; }, { createdAt = "09/16/2014"; 文字=南庄; }, { createdAt = "2014 年 10 月 23 日"; text = "家乐福,内湖"; }, { createdAt = "09/12/2014"; text = "元吉早午餐"; }, { createdAt = "09/16/2014"; text = "这里的食物出奇的好"; }, { createdAt = "09/16/2014"; text = "新州南寮港"; }, { createdAt = "09/10/2014"; text = "雅娜的家"; }, { createdAt = "09/10/2014"; text = "王龙佩"; } )
【问题讨论】:
-
等等...所以你想要一个 NSDate 的表示但没有时间?是这个问题吗?
-
不,我对它的表示没有问题,因为我可以使用格式化程序。我在 For 循环条件语句中有问题。
-
你的实际目标是什么?您是否要按日期对项目进行分组? IE。第 1 天 10 件,第 2 天 5 件?
-
@StephenFurlani 完全正确
标签: ios objective-c nsdate