-1

I would like a group element in NSMutableArray like this question: How to implement "group by values" in NSMutableArray?

But, my problem is inside the NSMutableArray i have no values ​​but I have an object of a class model like:

@interface up : NSObject 

@property (nonatomic,strong) NSString *id;
@property (nonatomic,strong) NSString *name;

@end

Any ideas?

Example:

Well, practically I have an NSMutableArray and in every cell of this array I have an object of a class model. What I have to do is create a new data structure where I merge objects based on the ID. For example:

{
 id = 1;
 name = "test"; 

}
{
 id = 1;
 name = "test1"; 
}
{
 id = 2;
 name = "test2";
}
{
 id = 2; 
 name = "test3"; 
}
{
 id = 3;
 name = "test4"; 
}

Result:

    {
 id = 1;
 name = "test"; 
 name = "test1"; 
}
{
 id = 2;
 name = "test2";
 name = "test3"; 

}
{
 id = 3;
 name = "test4"; 
}
R. Mohan
  • 2,182
  • 17
  • 30
Luca tom
  • 29
  • 7

1 Answers1

0
    NSMutableDictionary<NSString *, NSMutableArray<NSString *> *> *newDict =  NSMutableDictionary.new;
    for( UP *up in data) {
        if (!newDict[up.id]) {
            newDict[up.id] = NSMutableArray.new;
        }
        [newDict[up.id] addObject:up.name];
    }

newDict is the new data structure.

Noot
  • 44
  • 1
  • 3