這裏下面是我的部分代碼:的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月8日00:00:56.218 createdAt ------>二〇一四年十月一十四日十三點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創建時間 - > 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 = 」2014年9月4日「; 文本= 」梅劃弧湖「; },{ createdAt = 」2014年9月16日「; 文本= Nanjuang; },{ createdAt = 「2014年10月23日」; 文本= 「家樂福,內湖」; },{ createdAt = 「2014年9月12日」; 文本= 「Yuangi早午餐」; }, { createdAt =「09/16/2014」; text =「令人驚訝的非常好的食物在這裏」; }, { createdAt =「09/16/2014」; text =「南寮港,hsinju」; },{ createdAt = 「2014年9月10日」; text =「雅娜的家」; }, { createdAt =「09/10/2014」; text =「王長培」; } )
等等...所以你想有一個NSDate的代表,但沒有時間?這是問題嗎? – 2014-12-07 16:18:08
不,我沒有問題,因爲我可以使用格式化程序。 For循環條件語句中有問題。 – SanitLee 2014-12-07 16:20:49
你的實際目標是什麼?你是否試圖按日期分組項目?即第1天10件,第2天5件? – 2014-12-07 16:21:34