ios - NSMutableArray changing old stored data issue -
i parsing json , storing data nsmutablearray
on homepage. want access data on other page not after another, using singleton pattern sharing common data in homepage. storing nsmutablearray data singleton file common access. in second page showing data in tableview
. there 1 button modifies data not change original data i.e homepage arraydata
. storing data clonearray
, updating clonearray
. when return homepage , in viewdidappear
after placing breakpoint observing original data changed.
please have on issue , correct me doing wrong code.
for more reference added project link please download , check this: [project link][1]
in project have created 2 view controllers , 1 model class, , 1 singleton common data across file.
view controller:
in file parsing json , storing in nsmutablearray , setting same array data singleton file array common access across file.
secondviewcontroller:
in file getting array data singleton file , storing in local array.
there 1 button modify data. in modify data function modifying data , storing in other array common access (clonearray
).
but when return first view controller , in viewdidappear
after placing breakpoint, observing original data changed. please check, don't want original data changed. please check , suggest me doing wrong code.
your problem because property( viewcontroller->shippingarray
, secondpagecontroller->myshippingarray
, persondtklsingleton->shippingaddress
) point same array.
you can fix problem clone array deep copies, copy original new array. make sure items in original array adopted nscopying
.
secondpagecontroller
@implementation secondpagecontroller ... - (void)viewdidload { [super viewdidload]; myshippingarray=[[nsmutablearray alloc]init]; // deep copy self.myshippingarray = [[nsmutablearray alloc] initwitharray:[[persondtklsingleton sharedinstance]shippingaddress] copyitems:yes]; // additional setup after loading view. } ... @end
personmodel
@implementation personmodel @synthesize housernumber,resident,street,city,pincode,defaultaddress; - (id)copywithzone:(nszone *)zone { personmodel *copyobj = [personmodel new]; copyobj.housernumber = [self.housernumber copywithzone:zone]; copyobj.resident = [self.resident copywithzone:zone]; copyobj.street = [self.street copywithzone:zone]; copyobj.city = [self.city copywithzone:zone]; copyobj.pincode = [self.pincode copywithzone:zone]; copyobj.defaultaddress = [self.defaultaddress copywithzone:zone]; return copyobj; } @end
Comments
Post a Comment