mardi 4 août 2015

Nesting NSDictionary based on value

i've data from Library that return this:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:idChallengerN,@"idChallenger",dateFrom, @"date", nil];

[_array addObject:dict];

If i print _array this is there result:

_array = (
{
date = "2015-07-31 14:50:40 +0000";
idChallenger = 43;
},
{
date = "2015-07-31 19:30:00 +0000";
idChallenger = 22;
}
)

and it's ok, now I should get each 'date' and group this _array based on weeks...

I've tried:

NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];   

for (int i = 0; i<_array.count; i++) {

    NSDictionary *dict = [_array objectAtIndex:i];

    NSDate *date = [dict objectForKey:@"date"];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [calendar components:NSWeekOfYearCalendarUnit fromDate:date];

    NSLog(@"date week = %ld",(long)[dateComponents weekOfYear]);

    NSNumber *weekN = [NSNumber numberWithInteger:[dateComponents weekOfYear]];

    if ([tempDic objectForKey:weekN]) {

       //contains
    }
    else{

      //not contains
    }

weekN return the number of the week in year based on 'date',

now i'm stuck to how group certain data if have the same number of week, like this for example:

weekN = 31 {
  {
  idChallenger = 43;
  idChallenger = 22;
  }
}
weekN = 32 {
  {
  idChallenger = 55;
  idChallenger = 21;
  idChallenger = 678;
  }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire