通过对具有匹配id号的对象进行分组来重建NSArray? [英] Rebuild an NSArray by grouping objects that have matching id numbers?

查看:96
本文介绍了通过对具有匹配id号的对象进行分组来重建NSArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSArray,数组中的每个对象都有一个groupId和一个名字。每个对象都是唯一的,但有许多具有相同的groupId。有没有办法可以将阵列撕开并重建它,以便将名称分组为具有相应groubId的单个对象?以下是数组当前的样子:

I have an NSArray and each object in the array has a groupId and a name. Each object is unique but there are many with the same groupId. Is there a way i can tear the array apart and rebuild it so that the names are grouped into a single object with the corresponding groubId? Here is what the array currently looks like:

2013-03-12 20:50:05.572 appName[4102:702] the  array:  (
        {
        groupId = 1;
        name = "Dan";
    },
        {
        groupId = 1;
        name = "Matt";
    },
        {
        groupId = 2;
        name = "Steve";
    },
        {
        groupId = 2;
        name = "Mike";
    },
        {
        groupId = 3;
        name = "John";

    },
        {
        groupId = 4;
        name = "Kevin";
    }
)

这就是我想要的样子:

2013-03-12 20:50:05.572 appName[4102:702] the  array:  (
        {
        groupId = 1;
        name1 = "Dan";
        name2 = "Matt";
    },
        {
        groupId = 2;
        name1 = "Steve";
        name2 = "Mike";
    },
        {
        groupId = 3;
        name = "John";

    },
        {
        groupId = 4;
        name = "Kevin";
    }
)

编辑:
我试过& amp ;经过多次尝试失败了,大多数都是这样的事情(草率娱乐,但要提出一个想法):

I've tried & failed with many attempts, most along the lines of something like this (sloppy recreation, but to give an idea):

int idNum = 0;
for (NSDictionary *arrObj in tempArr){
    NSString *check1 = [NSString stringWithFormat:@"%@",[arrObj valueForKey:@"groupId"]];
    NSString *check2 = [NSString stringWithFormat:@"%@",[[newDict valueForKey:@"groupId"]];
    if (check1 == check2){
        NSString *nameStr = [NSString stringWithFormat:@"name_%d",idNum];
        [newDict setValue:[arrObj valueForKey:@"name"] forKey:nameStr];
    }
    else {
        [newDict setValue:arrObj forKey:@"object"];
    }
    idNum++;
}  


推荐答案

NSArray *array = @[@{@"groupId" : @"1", @"name" : @"matt"},
                   @{@"groupId" : @"2", @"name" : @"john"},
                   @{@"groupId" : @"3", @"name" : @"steve"},
                   @{@"groupId" : @"4", @"name" : @"alice"},
                   @{@"groupId" : @"1", @"name" : @"bill"},
                   @{@"groupId" : @"2", @"name" : @"bob"},
                   @{@"groupId" : @"3", @"name" : @"jack"},
                   @{@"groupId" : @"4", @"name" : @"dan"},
                   @{@"groupId" : @"1", @"name" : @"kevin"},
                   @{@"groupId" : @"2", @"name" : @"mike"},
                   @{@"groupId" : @"3", @"name" : @"daniel"},
                   ];

NSMutableArray *resultArray = [NSMutableArray new];
NSArray *groups = [array valueForKeyPath:@"@distinctUnionOfObjects.groupId"];
for (NSString *groupId in groups)
{
    NSMutableDictionary *entry = [NSMutableDictionary new];
    [entry setObject:groupId forKey:@"groupId"];

    NSArray *groupNames = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"groupId = %@", groupId]];
    for (int i = 0; i < groupNames.count; i++)
    {
        NSString *name = [[groupNames objectAtIndex:i] objectForKey:@"name"];
        [entry setObject:name forKey:[NSString stringWithFormat:@"name%d", i + 1]];
    }
    [resultArray addObject:entry];
}

NSLog(@"%@", resultArray);

输出:

    (
        {
        groupId = 3;
        name1 = steve;
        name2 = jack;
        name3 = daniel;
    },
        {
        groupId = 4;
        name1 = alice;
        name2 = dan;
    },
        {
        groupId = 1;
        name1 = matt;
        name2 = bill;
        name3 = kevin;
    },
        {
        groupId = 2;
        name1 = john;
        name2 = bob;
        name3 = mike;
    }
 )

这篇关于通过对具有匹配id号的对象进行分组来重建NSArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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