按date分组NSDictionary
我有一个NSDictionary,我想分组创builddate对象的数组
主NSDictionary的例子:
({ date = "2014-04-27"; group = "yellow.png"; length = 180; }, { date = "2014-04-28"; group = "blue.png"; length = 180; }, { date = "2014-04-27"; group = "blue.png"; length = 120; })
我想分组的东西类似于:
2014-04-27 = ( { date = "2014-04-27"; group = "yellow.png"; length = 180; }, { date = "2014-04-27"; group = "blue.png"; length = 180; }) 2014-04-28 = ( { date = "2014-04-28"; group = "blue.png"; length = 120; })
有人可以帮我吗? 我已经尝试了很多,但我不能得到它
看起来好像您的原始数据结构是一个字典数组。 你的问题措词不正确? 我看到每个单独的字典,但他们没有在顶级数据结构中的任何东西的关键。
假设是这样的(你有一个名为originalArray
的数组
NSMutableDictionary *dictionaryByDate = [NSMutableDictionary new]; for(NSDictionary *dictionary in originalArray) { NSString *dateString = dictionary[@"date"]; NSMutableArray *arrayWithSameDate = dictionaryByDate[dateString]; if(! arrayWithSameDate) { arrayWithSameDate = [NSMutableArray new]; dictionaryByDate[dateString] = arrayWithSameDate; } [arrayWithSameDate addObject: dictionary]; }
到此为止, dictionaryByDate
将是数组的字典(键入date)(给定数组中的所有对象将是具有相同date的字典)。