The response of my service is saved in a NSMutableArray
and also in some global class for using it in future viewcontrollers
.
todoDetailArray = [[tempResponse objectForKey:@"GetToDoListResult"]
objectForKey:@"EntityList"];
[[CommonClass sharedInstance] setTodoListDetailArray:[[tempResponse
objectForKey:@"GetToDoListResult"] objectForKey:@"EntityList"]];
when ever an object deleted from todoDetailArray
, the same object in [[CommonClass sharedInstance] TodoListDetailArray]
is also getting deleted.
Please help here, why this behaviour from NSArray
.
Thanks in advance..
If what you want is that items should not be removed from TodoListDetailArray
when you remove them from todoDetailArray
, then just copy the array over to your shared class:
[[CommonClass sharedInstance] setTodoListDetailArray:[[[tempResponse
objectForKey:@"GetToDoListResult"] copy] objectForKey:@"EntityList"]];
By doing like this, the two arrays are independent on one another. Otherwise, there will be just one single array, so when you modify it using one reference it will appear modified when accessing it from the other reference.
Both array sharing a common reference so change in one array reflect in other. So you can do like this
todoDetailArray = [[tempResponse objectForKey:@"GetToDoListResult"]
objectForKey:@"EntityList"];
[[CommonClass sharedInstance] setTodoListDetailArray:[todoDetailArray mutableCopy]];
mutableCopy for mutable array.
Its because, both shares a shared reference to a same memory address. So if you want both will independent of each other you just need to make a deep copy of it. Now its just doing a shallow copy. (A deep copy duplicates the objects referenced while a shallow copy duplicates only the references to those objects.)
So as listed above:
todoDetailArray = [[tempResponse objectForKey:@"GetToDoListResult"] objectForKey:@"EntityList"]; [[[CommonClass sharedInstance] setTodoListDetailArray:[[[tempResponse objectForKey:@"GetToDoListResult"] objectForKey:@"EntityList"] mutableCopy]];;
Now both todoDetailArray
and [CommonClass sharedInstance]
have different copy of objects.
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.