0

I have this lines of code:

- (void)redrawGraphWithArray:(NSArray *)array {
    _graphView.graphPoints = [array copy];
    [_graphView setNeedsDisplay];
}

graphPoints is NSArray too.

If I use

_graphView.graphPoints = array;

the graph looks same and there's no problem.

I wanna know which is the difference between

_graphView.graphPoints = [array copy] and

_graphView.graphPoints = array;

Thanks.

3 Answers 3

1

_graphView.graphPoints = array; assigns graphPoints to be the same array as the original object. You're just creating another pointer to the exact same memory. So for instance, this will happen:

NSMutableArray *array = [[NSMutableArray alloc] init];
array[0] = @{@"x": @1, @"y": @2};
array[1] = @{@"x": @1, @"y": @3};
_graphView.graphPoints = array;

// Now let's make some changes to the original array
// Note that I used an NSMutableArray to explain fully the differences between
// the two assignment types.
array[0] = @{@"x": @1, @"y": @1}; // With an NSArray you wouldn't be able to do this
array[1][@"y"] = @5;              // But this can still happen

// What happens to graphPoints?
_graphView.graphPoints[0]; // <== {x: 1, y: 1}
_graphView.graphPoints[1]; // <== {x: 1, y: 5}

In this case, graphPoints points to the exact same object as array so the two remain exactly the same. It's not illustrated in the code example, but it's very important to remember that this "sameness" points in both directions, so making changes to graphPoints will also change array.

On the other hand, [array copy] creates a new array object that is a copy of the original, so the above code will have a different result:

NSMutableArray *array = [[NSMutableArray alloc] init];
array[0] = @{@"x": @1, @"y": @2};
array[1] = @{@"x": @1, @"y": @3};
_graphView.graphPoints = [array copy];

// Now let's make the same changes to the original array
array[0] = @{@"x": @1, @"y": @1};
array[1][@"y"] = @5;

// What happens to graphPoints?
_graphView.graphPoints[0]; // <== {x: 1, y: 2}
_graphView.graphPoints[1]; // <== {x: 1, y: 5}

The first object didn't change in graphPoints, because we wrote a whole new object into array. Since graphPoints is now a copy, it didn't get affected.

However, the second object did change, because we didn't write a new object into array, but instead modified the existing object, which is contained by both arrays. This illustrates an important subtlety with object copying. This is what's known as a "shallow" copy, which means that the container gets copied, but its contents do not. You end up with two arrays, but only one set of contained objects.

There are relatively straightforward ways to also copy all of the contained objects by making sure that the contained objects all implement the NSCopying protocol (if you're just using Foundation classes like NSDictionary, NSArray, NSNumber, NSString, you don't need to worry, as this is already done for you) and using the - initWithArray:copyItems: initializer on NSArray. However even this will create shallow copies of the contained objects, but there is lots of info available about how to implement a full "deep" copy, should you need to do so.

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

Comments

0

[array copy] will create new instance of array(with same content) and assign it to graphpoints. Where as if you just assign array, graphpoints will have same reference as that of object array. That is, both will point to the same object. Thus if you change array object then it will change graphpoints too. But that won't be the case if you use copy.

Comments

0

It is a premature optimisation.

The main difference: the first approach results in an autoreleased "copy" that you don't own and don't have to release, while you do own the object created on the second line. Both arrays will be immutable, by the way.

Comments

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.