Copy property attribute Objective-C creates a deep copy or shallow copy?

I have an NSMutabelArray with some objects in it as a property of my View-Controller . Now I pass this array when I push this VC.

I originally assigned it.

but now I needed to modify the array whenever the device spins, in VC, without affecting the one in the previous VC. so he made a copy property.

Now I individually changed the objects in the array.

but somehow ... the array in my previous VC was also changing.

I had to create a new NSMutabelArray with modified objects and had to set its property.

Is this copy attribute just another array with the same object references added to the array?

+4
source share
2 answers

The copy attribute means that it will be assigned the object returned after sending the copy message to the object that was transferred. What means deep or shallow depends on the object being copied. Foundation collections create small copies because it allows copy be implemented as retain for immutable collections.

+10
source

Is this copy attribute just another array with the same object references added to the array?

Yes, how does it work.

There are several suggestions for deep copying an array in this answer . Maybe one of them will work for you?

+3
source

Source: https://habr.com/ru/post/1399817/


All Articles