ios - Combine data of objects in NSMutableArray with same key -


i have following nsmutablearray keys 'id, value, name' , 'id, animal' :

    <__nsarraym 0x7a64f450>(     {         id = ccc;         value = 11;         name = "greg";     },     {         id = bbb;         value = 2;         name = "dan";     },     {         id = aaa;         value = 23;         name = "mary";     },     {         id = aaa;         animal = "dog";     },     {         id = ccc;         animal = "cat";     }  ) 

is possible merge content matching keys? in end i'd have:

        <__nsarraym 0x7a64f450>(         {             id = ccc;             value = 11;             name = "greg";             animal = "cat"         },         {             id = bbb;             value = 2;             name = "dan";         },         {             id = aaa;             value = 23;             name = "mary";             animal = "dog";         }      ) 

i'm looking combine objects if keys match id. other keys should merged together.

a more elegant way use nspredicate. use code. arrrecords storing records before merge.

nsstring *strkey;   nspredicate *predicate;   nsarray *temparray;         for(int = 0; < [arrrecords count]; i++) {     strkey = [[arrrecords objectatindex:i] valueforkey:@"id"];     predicate = [nspredicate predicatewithformat:@"id == %@",strkey];     temparray = [arrrecords filteredarrayusingpredicate:predicate];     if([temparray count] > 1) {        for(int j =0; j < [temparray count]-1; j++) {        [[arrrecords objectatindex:i] addentriesfromdictionary:[temparray objectatindex:j+1]];        [arrrecords removeobject:[temparray objectatindex:j+1]];  // need remove record super set otherwise duplicated again.             }         }     } 

edit - code nsdictionary objects

nsmutablearray *newrecords = [[nsmutablearray alloc] init]; // add before method body   for(int = 0; < [arrrecords count]; i++) {         strkey = [[arrrecords objectatindex:i] valueforkey:@"id"];        predicate = [nspredicate predicatewithformat:@"id == %@",strkey];        temparray = [arrrecords filteredarrayusingpredicate:predicate];        if([temparray count] > 1) {            nsmutabledictionary *tempdict = [[nsmutabledictionary alloc] init];             for(int j =0; j < [temparray count]; j++) {                [tempdict addentriesfromdictionary:[temparray objectatindex:j]];                if(j!=0) {                   [arrrecords removeobject:[temparray objectatindex:j]];                }                          }            [newrecords addobject:tempdict];      }      else {         [newrecords addobject:[arrrecords objectatindex:i]];      }    } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -