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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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, Check this stackoverflow.com/questions/4089238/implementing-nscopying. It is already answered here.
ok thanks i have another question, implementing the NSCopying protocol works only when i call initArray copyItems, in the other cases work normally right?
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 developer.apple.com/library/mac/#documentation/Cocoa/Reference/…
Since the original array contains Core Data objects, it should be noted that NSManagedObject does not implement the NSCopying protocol.
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

1 Comment

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 :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.