ios - Can't make NSArray deep copy -
a user on app can select filters narrow down items. once filters selected, nsnotification sent passes array of selected filters (nsstrings) view controller.
when receive notification, can extract array [notification userinfo]. now, want compare new filters saved user, make call server if different. here's method:
- (void)applyfilters:(nsnotification *)notification { nsarray *filters = [[notification userinfo] objectforkey:@"filters"]; if (showmyitems) { if (![[nsset setwitharray:filters] isequaltoset:[nsset setwitharray:[[pfuser currentuser] objectforkey:@"filtersmy"]]]) { [[pfuser currentuser] setobject:[nsarray arraywitharray:filters] forkey:@"filtersmy"]; [[pfuser currentuser] saveinbackground]; } } else { if (![[nsset setwitharray:filters] isequaltoset:[nsset setwitharray:[[pfuser currentuser] objectforkey:@"filters"]]]) { [[pfuser currentuser] setobject:[nsarray arraywitharray:filters] forkey:@"filters"]; [[pfuser currentuser] saveinbackground]; } } } here i'm comparing nssets rather nsarrays because order of objects/strings not important. try create new array , set user's new filters via arraywitharray:. so, when next time user changes filter selection, expect new filters array different 1 saved user (my if statement condition). however, when nslog both (new , saved/fetched arrays), same! , therefore if condition never passes.
my explanation of [nsarray arraywitharray:filters] somehow still pointing filters array.
i tried [nskeyedunarchiver unarchiveobjectwithdata:[nskeyedarchiver archiveddatawithrootobject:filters]] suggested on , apple, result same.
can please spot problem? i'm out of options , ideas...
update:
i have pasted full method (above). bit more background:
- i using 2 instances of same
uiviewcontrollerclass - in 1 instance,
showmyitemssetyes(so top part ofifstatement called). in instance -no, lower part called - what interesting, when
showmyitemsno, code works expected! whenshowmyitemsyes, doesn't... although code identical.
any thoughts?
nsarray *temp = [[nsmutablearray alloc] initwitharray:filters copyitems:yes]; (the above works one-level-deep copy)
for deep copying best way i've found far is
nsmutablearray *deepcopyarray = (nsmutablearray*)cfbridgingrelease(cfpropertylistcreatedeepcopy(kcfallocatordefault, (cfdictionaryref)filters, kcfpropertylistmutablecontainers));
Comments
Post a Comment