Przebuduj NSArray, grupując obiekty, które mają pasujące numery identyfikacyjne?
Mam NSArray i każdy obiekt w tablicy ma groupId i nazwę. Każdy obiekt jest unikalny, ale jest wiele takich samych grup. Czy istnieje sposób, aby rozerwać tablicę i odbudować ją tak, aby nazwy były zgrupowane w jednym obiekcie z odpowiednim modułem? Oto, jak wygląda obecnie tablica:
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";
}
)
Chciałbym, aby wyglądało to tak:
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";
}
)
EDYCJA: Próbowałem i nie powiodło się z wieloma próbami, najczęściej na wzór czegoś takiego (niechlujna rekreacja, ale żeby dać pomysł):
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++;
}