1

i have two nsmutablearray:

@property (nonatomic, strong) NSMutableArray *calSeries;
@property (nonatomic, strong) NSMutableArray *calSeries2Copy;

then i do this:

self.calSeries = [self getSeries];
self.calSeries2Copy = [NSMutableArray arrayWithArray:self.calSeries];

the getSeries method fetchObject from the core data, but if i change an element in the calSeries, it change also in the calSeries2Copy, how i can create two array separately so as when i change an element in one array don't change also in the other array?

Piero
  • 9,173
  • 18
  • 90
  • 160

2 Answers2

3

Try this,

self.calSeries2Copy = [[NSMutableArray alloc] initWithArray:self.calSeries copyItems:YES];

As per documentation, this should copy if you have implemented NSCopying protocol.

flag

If YES, each object in array receives a copyWithZone: message to create a copy of the object—objects must conform to the NSCopying protocol. In a managed memory environment, this is instead of the retain message the object would otherwise receive. The object copy is then added to the returned array.

If NO, then in a managed memory environment each object in array simply receives a retain message when it is added to the returned array.

Discussion

After an immutable array has been initialized in this way, it cannot be modified.

The copyWithZone: method performs a shallow copy. If you have a collection of arbitrary depth, passing YES for the flag parameter will perform an immutable copy of the first level below the surface. If you pass NO the mutability of the first level is unaffected. In either case, the mutability of all deeper levels is unaffected.

Community
  • 1
  • 1
iDev
  • 23,310
  • 7
  • 60
  • 85
  • give me this error [Show copyWithZone:]: unrecognized selector sent to instance 0xc0ad8a0 Show is the element in the array that is fetched from the core data, how i can implement the NSCopying? – Piero Dec 09 '12 at 11:14
  • @Piero, Check this http://stackoverflow.com/questions/4089238/implementing-nscopying. It is already answered here. – iDev Dec 09 '12 at 11:15
  • ok thanks i have another question, implementing the NSCopying protocol works only when i call initArray copyItems, in the other cases work normally right? – Piero Dec 09 '12 at 11:20
  • It is basically for copying operations. If you want to create a complete copy of an object, this protocol is useful. Check this documentation for more details https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSCopying_Protocol/Reference/Reference.html – iDev Dec 09 '12 at 11:23
  • Since the original array contains Core Data objects, it should be noted that `NSManagedObject` does *not* implement the `NSCopying` protocol. – Martin R Dec 09 '12 at 12:10
0

copying an array by default makes a shallow copy only. This means that the array is copied but the elements are only retained once more

Copy the items with [[NSMutableArray alloc] initWithArray:srcArray copyItems:YES]; to get a deep copy where also every element is copied

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Just a note: A *deep copy* is a copy where recursively all elements are copied. This method copies only the first level objects. (Did not downvote though :-) – Martin R Dec 09 '12 at 12:15