Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebuild an NSArray by grouping objects that have matching id numbers?

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

This is what I would like it to look like:

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

EDIT: 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++;
}  
like image 510
Daniel McCarthy Avatar asked Mar 13 '13 01:03

Daniel McCarthy


People also ask

What is NSArray?

NSArray is an immutable Objective C class, therefore it is a reference type in Swift and it is bridged to Array<AnyObject> . NSMutableArray is the mutable subclass of NSArray .

What's a difference between NSArray and NSSet?

An NSSet is much like an NSArray, the only difference is that the objects it holds are not ordered. So when you retrieve them they may come back in any random order, based on how easy it is for the system to retrieve them.

How do I create an NSArray in Objective-C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.

Can NSArray contain nil?

arrays can't contain nil. There is a special object, NSNull ( [NSNull null] ), that serves as a placeholder for nil.


2 Answers

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

Output:

    (
        {
        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;
    }
 )
like image 56
Sergey Kuryanov Avatar answered Oct 15 '22 03:10

Sergey Kuryanov


This calls for a NSDictionary of NSArrays. There's no quick and elegant way - you'd have to scroll through the source.

NSMutableDictionary *d = [NSMutableDictionary dictionaryWithCapacity:10]; //Or use alloc/init
for(SomeObject o in appname) //What's the type of objects? you tell me
{
    NSObject *ID = [o objectForKey: @"groupId"];
    NSMutableArray *a = [d objectForKey: ID];
    if(a == nil)
    {
        a = [NSMutableArray arrayWithCapacity: 10];
        [d setObject:a forKey: ID];
    }
    [a addObject: [o objectForKey: @"name"]];
}

EDIT: edited to not assume the datatype of the key.

like image 20
Seva Alekseyev Avatar answered Oct 15 '22 03:10

Seva Alekseyev