-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"; 
}
2
  • Is this what you want? Commented Oct 12, 2017 at 23:42
  • generating your expected result is not possible; what kinda output you'd like to get eventually? Commented Oct 13, 2017 at 8:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god. Work perfectly! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.