The copy and mutableCopy Methods

The Foundation classes implement methods known as copy and mutableCopy that you can use to create a copy of an object. This is done by implementing a method in conformance with the <NSCopying> protocol for making copies. If your class needs to distinguish between making mutable and immutable copies of an object, you must implement a method according to the <NSMutableCopying> protocol as well. You learn how to do that later in this section.

Getting back to the copy methods for the Foundation classes, given the two NSMutableArray objects dataArray2 and dataArray, as described in the previous section, the statement

dataArray2 = [dataArray mutableCopy];

creates a new copy of dataArray ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.