2014-12-07 75 views
0

這裏下面是我的部分代碼:的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 =「王長培」; } )
+0

等等...所以你想有一個NSDate的代表,但沒有時間?這是問題嗎? – 2014-12-07 16:18:08

+0

不,我沒有問題,因爲我可以使用格式化程序。 For循環條件語句中有問題。 – SanitLee 2014-12-07 16:20:49

+0

你的實際目標是什麼?你是否試圖按日期分組項目?即第1天10件,第2天5件? – 2014-12-07 16:21:34

回答

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]]; 
}]; 
+0

我得到這個錯誤: 2014-12-08 01:10:02.365 - [__ NSDate長度]:無法識別的選擇器發送到實例0x192094e0 2014-12-08 01:10:02.368 ***終止應用程序到期未捕獲的異常「NSInvalidArgumentException」,原因是:「 - [__ NSDate的長度]:無法識別的選擇發送到實例0x192094e0」 – SanitLee 2014-12-07 17:17:20

+0

我想你的代碼沒有完全瞭解它,我得到了上述錯誤。我基本上把你的代碼放在我的[postQuery ..]塊中。 – SanitLee 2014-12-07 17:21:04

+0

@SanitLee什麼是[obj createdAt]?它是一個NSDate嗎? – 2014-12-07 17:40:19