objective c - Editing one object in an NSMutableArray also changes another object in the NSMutableArray -
i had navigation application working normally. in table view, last item called "add item", , if user pressed it, create new object , pass view user enter details object. when user returned previous screen, new object show in array displayed in table.
i changed "add item" field first field in table, not last. made appropriate changes array display correctly on table. however, noticing strange behavior.
if edit first object in array, 7th object changes same object. if edit second object in array, fourth , sixth object change same. if edit third item in array, fifth object changes same.
what happening?
in viewdidload: method initialize object this:
persondetails *persondetails = [[persondetails alloc] init];
this method gets executed when user selects row on table
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // navigation logic may go here. create , push view controller. updatepersonarray = yes; arrayindex = indexpath.row-1; editperson = [[editclasscontroller alloc] initwithnibname:@"editperson" bundle:nil]; editperson.title = @"edit person"; if (arrayindex != -1) { persondetails = [classarray objectatindex:arrayindex]; } else { persondetails = [[persondetails alloc] init]; } editperson.persondetails = persondetails; [self.navigationcontroller pushviewcontroller:editperson animated:yes]; [editperson release];
}
this viewwillappear looks like. update table after object has been edited.
- (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; if ([persondetails isempty]) { updatepersonarray = no; } if (updatepersonarray) { if (arrayindex == -1) { nslog(@"adding new object array"); [personarray addobject:persondetails]; } else { nslog(@"replacing object @ index %d", arrayindex); [personarray replaceobjectatindex:arrayindex withobject:persondetails]; } [self savearraytodisk]; [self.tableview reloaddata]; updatepersonarray = no; } else { //load array disk nslog(@"loading array disk"); nsdata *thedata = [[nsuserdefaults standarduserdefaults] objectforkey:@"personarray"]; if (thedata != nil) { nslog(@"found something"); personarray = [[nsmutablearray alloc] initwitharray:[nskeyedunarchiver unarchiveobjectwithdata:thedata]]; } else { personarray = [[nsmutablearray alloc] init]; } } }
edit: solve problem implementing nscopy person object , making copy of object array instead of directly pointing object in array. know why solved problem?
edit: solve problem implementing nscopy person object , making copy of object array instead of directly pointing object in array. know why solved problem?
the original problem pretty guaranteed issue of having same persondetails in array multiple times. if like:
for (id p in myarray) nslog("%p", p);
i'd bet of addresses same, indicating same object in array multiple times.
which why copying object "fixed" problem. hiding dodgy logic led above situation making copy on every insertion.
Comments
Post a Comment