ios - Remove duplicate key value from array of NSDictionary? -
i need compare 2 array values 1 one, if value match 1 another, need update value , replace main array.
nsmutablearray having array of nsdictionary
nsarray having array of nsstring objects.
my code
nsmutabledictionary *dicone = @{@"eveid":@"1",@"imgarray":@[@"one",@"two"]}; nsmutabledictionary *dictwo = @{@"eveid":@"2",@"imgarray":@[@"three",@"four"]}; nsmutablearray *temparray = [nsmutablearray array]; [temparray addobject:dicone]; [temparray addobject:dictwo]; nsarray *eventarray = [nsarray arraywithobjects:@"1",@"2",@"3",nil]; nsstring *filename = @"five"; (int jj = 0; jj<[eventarray count]; jj++) { nsstring *toparrayeventid = [nsstring stringwithformat:@"%@",[eventarray objectatindex:jj]]; (int kk = 0; kk<[temparray count]; kk++) { nsmutabledictionary *curentobj = [[temparray objectatindex:kk]mutablecopy]; nsstring *innerarrayeventid = [nsstring stringwithformat:@"%@",[curentobj valueforkey:@"eveid"]]; if ([innerarrayeventid isequaltostring:toparrayeventid]) { nslog(@"update inner array , replace main array"); nsmutablearray *imgarray = [nsmutablearray array]; imgarray = [[curentobj objectforkey:@"imgarray"]mutablecopy]; [imgarray addobject:filename]; [curentobj setobject:[imgarray mutablecopy] forkey:@"imgarray"]; [temparray replaceobjectatindex:kk withobject:[curentobj mutablecopy]]; }else{ nslog(@"add new object main array"); nsmutabledictionary* storedata = [nsmutabledictionary dictionary]; [storedata setobject:toparrayeventid forkey:@"eveid"]; nsmutablearray *imgarray = [nsmutablearray array]; [imgarray addobject:filename]; [storedata setobject:imgarray forkey:@"imgarray"]; [temparray addobject:storedata]; } } } nslog(@"final updation------->%@",temparray);
i want output below
( { eveid = 1; imgarray = ( one, two, 5 ); }, { eveid = 2; imgarray = ( three, four, 5 ); } { eveid = 3; imgarray = ( 5 ); } )
but not getting code..its giving wrong output. having problem inner loop.
wrong output looks below
( { eveid = 1; imgarray = ( one, two, 5 ); }, { eveid = 2; imgarray = ( three, four, 5 ); }, { eveid = 1; imgarray = ( five, 5 ); }, { eveid = 2; imgarray = ( five, 5 ); }, { eveid = 2; imgarray = ( five, 5 ); }, { eveid = 3; imgarray = ( five, 5 ); }, { eveid = 3; imgarray = ( five, 5 ); }, { eveid = 3; imgarray = ( five, 5 ); }, { eveid = 3; imgarray = ( five, 5 ); }, { eveid = 3; imgarray = ( five, 5 ); } )
predicate method using achieve this
nsmutabledictionary *dicone = @{@"eveid":@"1",@"imgarray":@[@"one",@"two"]}; nsmutabledictionary *dictwo = @{@"eveid":@"2",@"imgarray":@[@"three",@"four"]}; nsmutablearray *temparray = [nsmutablearray array]; [temparray addobject:dicone]; [temparray addobject:dictwo]; nsarray *eventarray = [nsarray arraywithobjects:@"1",@"2",@"3",nil]; nsstring *filename = @"five"; (int jj = 0; jj<[eventarray count]; jj++) { nsarray *resultarray = [temparray filteredarrayusingpredicate:[nspredicate predicatewithformat:@"self.eveid = %@", [eventarray objectatindex:jj]]]; nsstring *toparrayeventid = [nsstring stringwithformat:@"%@",[eventarray objectatindex:jj]]; if([resultarray count]>0) { nsmutabledictionary *curentobj; nsstring *innerarrayeventid = [nsstring stringwithformat:@"%@",[[resultarray objectatindex:0] valueforkey:@"eveid"]]; if ([innerarrayeventid isequaltostring:toparrayeventid]) { curentobj= [[resultarray objectatindex:0]mutablecopy]; nsmutablearray *imgarray = [nsmutablearray array]; imgarray = [[[resultarray objectatindex:0] objectforkey:@"imgarray"]mutablecopy]; [imgarray addobject:filename]; [curentobj setobject:[imgarray mutablecopy] forkey:@"imgarray"]; [temparray replaceobjectatindex:jj withobject:[curentobj mutablecopy]]; } } else { nsmutabledictionary* storedata = [nsmutabledictionary dictionary]; [storedata setobject:toparrayeventid forkey:@"eveid"]; nsmutablearray *imgarray = [nsmutablearray array]; [imgarray addobject:filename]; [storedata setobject:imgarray forkey:@"imgarray"]; [temparray addobject:storedata]; } } nslog(@"final updation------->%@",temparray); output : final updation------->( { eveid = 1; imgarray = ( one, two, 5 ); }, { eveid = 2; imgarray = ( three, four, 5 ); }, { eveid = 3; imgarray = ( 5 ); } )
Comments
Post a Comment