Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do copy and mutableCopy apply to NSArray and NSMutableArray?

What is the difference between copy and mutableCopy when used on either an NSArray or an NSMutableArray?

This is my understanding; is it correct?

// ** NSArray ** NSArray *myArray_imu = [NSArray  arrayWithObjects:@"abc", @"def", nil];  // No copy, increments retain count, result is immutable NSArray *myArray_imuCopy = [myArray_imu copy];  // Copys object, result is mutable  NSArray *myArray_imuMuta = [myArray_imu mutableCopy];  // Both must be released later 

// ** NSMutableArray ** NSMutableArray *myArray_mut = [NSMutableArray arrayWithObjects:@"A", @"B", nil];  // Copys object, result is immutable NSMutableArray *myArray_mutCopy = [myArray_mut copy];  // Copys object, result is mutable NSMutableArray *myArray_mutMuta = [myArray_mut mutableCopy];  // Both must be released later 
like image 366
fuzzygoat Avatar asked Jan 04 '10 20:01

fuzzygoat


People also ask

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

How do you declare NSArray in Objective C?

In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. NSArray *array = @[someObject, @"Hello, World!", @42]; In Objective-C, the compiler generates code that makes an underlying call to the arrayWithObjects:count: method.

What is NSMutableArray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray .

Can NSArray contain nil?

arrays can't contain nil.


1 Answers

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 a mutable copy, and mutate it NSMutableArray *mut = [arr mutableCopy]; [mut removeObject: @"one"]; 

Summary:

  • you can depend on the result of mutableCopy to be mutable, regardless of the original type. In the case of arrays, the result should be an NSMutableArray.
  • you cannot depend on the result of copy to be mutable! copying an NSMutableArray may return an NSMutableArray, since that's the original class, but copying any arbitrary NSArray instance would not.

Edit: re-read your original code in light of Mark Bessey's answer. When you create a copy of your array, of course you can still modify the original regardless of what you do with the copy. copy vs mutableCopy affects whether the new array is mutable.

Edit 2: Fixed my (false) assumption that NSMutableArray -copy would return an NSMutableArray.

like image 61
Asher Dunn Avatar answered Sep 20 '22 01:09

Asher Dunn