How do copy and mutableCopy apply to NSArray and NSMutableArray?

copy and mutableCopy are defined in different protocols (NSCopying and NSMutableCopying, respectively), and NSArray conforms to both. mutableCopy is defined for NSArray (not just NSMutableArray) and allows you to make a mutable copy of an originally immutable array: // create an immutable array NSArray *arr = [NSArray arrayWithObjects: @”one”, @”two”, @”three”, nil ]; // create … Read more

Implementing NSCopying

To implement NSCopying, your object must respond to the -copyWithZone: selector. Here’s how you declare that you conform to it: @interface MyObject : NSObject <NSCopying> { Then, in your object’s implementation (your .m file): – (id)copyWithZone:(NSZone *)zone { // Copying code here. } What should your code do? First, create a new instance of the … Read more