Nesting NSDictionary based on value

一世执手 提交于 2019-12-24 11:50:10

问题


i've data from Library that return this:

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

[_array addObject:dict];

If i print _array this is there result:

{
    date = "2015-07-31 14:50:40 +0000";
    distance = "-1";
    idChallenger = 43;
},
    {
    date = "2015-07-31 16:18:57 +0000";
    distance = "-1";
    idChallenger = "-1";
},
    {
    date = "2015-07-31 16:19:05 +0000";
    distance = "-1";
    idChallenger = "-1";
},

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;
  }
}

thanks to popctrl :

NSMutableArray *weekArray = [NSMutableArray array];

for(int i = 0; i < 52; i++){
    [weekArray addObject:[NSMutableArray array]];
}


//This replaces your for loop
for (int i = 0; i<_array.count; i++) {
    NSDictionary *dict = [_array objectAtIndex:i];

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

    NSCalendar *calendar = [NSCalendar currentCalendar];

    //Notice that I changed NSWeekOfYearCalendarUnit to NSCalendarUnitWeekOfYear, as NSWeekOfYearCalendarUnit has been deprecated
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitWeekOfYear fromDate:date];

    NSMutableArray *innerArray = weekArray[[dateComponents weekOfYear] - 1];
    [innerArray addObject:dict];
}

this code produce very well structure, but if I want divide weeks in year before?


回答1:


If you're looking for something that follows the example you've given at the bottom of your post, that would be an array of arrays, where the first array is indexed by date and the inner arrays have no specific order.

If you would like to preserve the initial dictionary data structure, just make the values contained in that array of arrays the dictionary.

EDIT: Here's the code I would use

//To initialize the array
NSMutableArray *weekArray = [NSMutableArray array];

for(int i = 0; i < 52; i++){
    [weekArray addObject:[NSMutableArray array]];
}


//This replaces your for loop
for (int i = 0; i<_array.count; i++) {
    NSDictionary *dict = [_array objectAtIndex:i];

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

    NSCalendar *calendar = [NSCalendar currentCalendar];

    //Notice that I changed NSWeekOfYearCalendarUnit to NSCalendarUnitWeekOfYear, as NSWeekOfYearCalendarUnit has been deprecated
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitWeekOfYear fromDate:date];

    NSMutableArray *innerArray = weekArray[[dateComponents weekOfYear] - 1];
    [innerArray addObject:dict];
}


来源:https://stackoverflow.com/questions/31815064/nesting-nsdictionary-based-on-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!