Reputation: 1753
I have been trying to understand the difference between deep and shallow copy by following the link: Difference
What i did :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tempArray=[[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G", nil];
shallowArray=[[NSMutableArray alloc] initWithArray:tempArray copyItems:NO];
deepArray=[[NSMutableArray alloc] initWithArray:tempArray copyItems:YES];
}
- (IBAction)testShallowDeep:(id)sender {
[tempArray removeObjectAtIndex:0];
NSLog(@"ShallowArray should get changed==%@",shallowArray);
NSLog(@"DeepArray should remain Same===%@'",deepArray);
}
However when i checked the logs both are coming same as temp array. Anybody can explain this concept with above example. According to my understanding Shallow Array should get changed and element 'A' should be removed from it and Deep Array should remain same as temp Array.
Upvotes: 0
Views: 797
Reputation: 4899
So what you're basically doing here, is creating three arrays with tempArray
and shallowArray
pointing to the same objects and deepArray
to copies.
Initially it's like this, after instantiating the arrays:
both tempArray
and shallowArray
point to the same objects. But the arrays themselves are different objects!
And after
[tempArray removeObjectAtIndex:0]
this is the situation:
The behavior that you seem to suspect, would be achieved by having shallowArray
point to the same object as tempArray
:
shallowArray = tempArray;
Upvotes: 6
Reputation: 8947
Well you are right with the code that you used here should work the same as you stated above. But the things I've found that might cause issue is that this method returns a newly allocated array and each object in the Array receives a copyWithZone:
message. The 1st part of the statement is strong to discuss your problem here because every array has it's own memory and at the same point Apple doc says this should have a shallow copy here (The link you gave above). I'm afraid you are using ARC here and in ARC copyWithZone: is not available. Which might be stopping array to have shallow copy here. That's mean the new array is not obtaining the zone of the old array. And if none of these case are; then some one must file a report to Apple to clarify such kind of things.
EDIT:
The answer to your question goes here. Everything is working fine and OK here. Yes It is shallow copy here. But one thing I've noticed here deep copy is also pointing to the same memory location both shallow and temp memory locations are. In your scenario. temparry's first object has memory location say 0x13df19. And the same is for shallow copy. It must be different for deeparray but sat my side it's same. Well coming to problem, When you remove the object from temp array. The objects adress is removed. According to documentation, objects of both shallow and temp array are pointing to the same locations. Not that the whole array is same. So when you remove object from temp array it is removed but the object will be in memory and will reside in each array it has been added. The scenario you are talking about would seem like this
someArray = tempArray;
[tempArray removeObjectAtIndex:0];
NSLog(@"Updated Array should get changed==%@",someArray);
now this will have the same array as temp array is because they are pointing to same memory locations.
Upvotes: 0