按日期分组 NSDictionary [英] Group NSDictionary by dates

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

问题描述

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

I have a NSDictionary and i want to group it creating an array of objects by date

主要 NSDictionary 示例:

Example of the main 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;

    })

我想将类似的东西归为一组:

I want to group something similar as:

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;

        })

有人可以帮我吗?我已经尝试了很多,但我无法得到它

Could someone help me? i have tried many FOR but i cant get it

推荐答案

看起来好像您的原始数据结构是一个字典数组.你的问题表述有误吗?我看到了每个单独的字典,但它们没有以顶级数据结构中的任何内容为键.

It appears as though your original data structure is an array of dictionaries. Was your question phrased incorrectly? I see each individual dictionary but they are not keyed on anything in the top level data structure.

假设是这种情况(您有一个名为 originalArray

Assuming that is the case (you have an array called 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 将是一个数组字典(以日期为键)(给定数组中的所有对象都将是具有相同日期的字典).

By the end of this, dictionaryByDate will be a dictionary (keyed on date) of arrays (all objects in a given array will be dictionaries with the same date).

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

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