ios - NSArray mutableCopy creates new array but still points to old contents -
i have nsmutablearray
called playersarray
in singleton class holds applications main datasource. each object of playersarray
nsdictionary
, content :
{ sgfid = 1; sgfplayer = "<playercontact: 0xbf851b0>"; }
playercontact
nsobject
subclass containing properties like: nsstring * playername, playerteam, bool playerselected , on.
in 1 of viewcontrollers, in viewdidload
, want take deep copy of playersarray
in nsmutablearray
named duplicateplayersarray
.
playersarray = [[sgfhelper sharedhelpers] sgfcontactsarray]; duplicateplayersarray = [playersarray mutablecopy];
now have 2 separate copies, under impression playersarray , duplicateplayersarray 2 totally different arrays in memory. found not!
even if debugger shows have different memory addresses, contents have same memory addresses. when this:
[((nsmutabledictionary *)[duplicateplayersarray objectatindex:0]) setobject:@"333" forkey:@"sgfid"];
playersarray's dictionary @ index:0 has "333" key "sgfid" instead of "1" used before above line of code ran.
but, if run below code, then, 2 arrays start differ
[duplicateplayersarray replaceobjectatindex:0 withobject:tempdict];
still doesn't address concern because 2 arrays wanted believe different still "connected". change in one, results in other array change contents.
can friends please show me way deep copy array explained contents of in way of contents kept in different objects.
use initwitharray:copyitems:
copy each entry in array
nsmutablearray *duplicateplayersarray = [[nsmutablearray alloc] initwitharray:playersarray copyitems:yes];
Comments
Post a Comment