Shallow copy only copies a memory address. Changing one object will also change another.
NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects:@1, @2, @3, nil]; NSMutableArray *secondArray = [[NSMutableArray alloc] initWithObjects:@4, @5, @6, nil]; secondArray = firstArray; [secondArray setObject:@9 atIndexedSubscript:0]; NSLog(@"%@", [firstArray objectAtIndex:0]); //9 the value in a first array has also changed
Deep copy copies the whole object. This process is slower, but both objects have their own copies, so changes in one will not affect another.
NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects:@1, @2, @3, nil]; NSMutableArray *secondArray = [[NSMutableArray alloc] initWithObjects:@4, @5, @6, nil]; secondArray = [firstArray mutableCopy]; [secondArray setObject:@9 atIndexedSubscript:0]; NSLog(@"%@", [firstArray objectAtIndex:0]); //1 the value in a first array has not changed