基于值嵌套 NSDictionary [英] Nesting NSDictionary based on value

查看:58
本文介绍了基于值嵌套 NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自图书馆的数据返回这个:

i've data from Library that return this:

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

[_array addObject:dict];

如果我打印_array,结果是:

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

没关系,现在我应该获取每个 date 并根据周对这个 _array 进行分组...

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

我试过了:

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 根据日期"返回一年中的周数,

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

感谢 popctrl :

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?

推荐答案

如果您正在寻找遵循您在帖子底部给出的示例的内容,那将是一个数组数组,其中第一个数组按日期索引,内部数组没有特定顺序.

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.

这是我会使用的代码

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

这篇关于基于值嵌套 NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆