Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

objective-c

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 initially made it assign.

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

now I was individually changing the objects in the array.

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

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

Is it that copy attribute just makes another array with same references of objects added to the array ?

like image 408
Amogh Talpallikar Avatar asked Dec 13 '22 04:12

Amogh Talpallikar


1 Answers

The copy attribute means the property will be assigned the object returned after sending the copy message to the object that was passed in. Whether that means deep or shallow depends on the object getting copied. The Foundation collections create shallow copies because that allows copy to be implemented as retain for immutable collections.

like image 60
Steven Kramer Avatar answered Jun 03 '23 00:06

Steven Kramer