4

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 ?

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135

2 Answers2

10

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.

Steven Kramer
  • 8,473
  • 2
  • 37
  • 43
3

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

Yes, that's how it works.

There's some suggestions for deep copying an array in this answer. Maybe one of those would work for you?

Community
  • 1
  • 1
Amy Worrall
  • 16,250
  • 3
  • 42
  • 65