按日期分组NSDictionary

Mig*_*ito 3 objective-c nsdictionary nsarray ios

我有一个NSDictionary,我想按日期分组创建一个对象数组

主要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;

    })
Run Code Online (Sandbox Code Playgroud)

我想分组类似的东西:

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;

        })
Run Code Online (Sandbox Code Playgroud)

有人能帮助我吗?我尝试了很多FOR,但我无法得到它

Dim*_*ima 6

看起来好像您的原始数据结构是一个字典数组.你的问题是不正确的?我看到每个单独的字典,但它们没有键入顶级数据结构中的任何内容.

假设是这种情况(你有一个名为的数组 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];
}
Run Code Online (Sandbox Code Playgroud)

到此结束时,dictionaryByDate将是数组的字典(键入日期)(给定数组中的所有对象将是具有相同日期的字典).