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 anNSMutableArray
. - you cannot depend on the result of
copy
to be mutable!copy
ing anNSMutableArray
may return anNSMutableArray
, since that’s the original class, butcopy
ing any arbitraryNSArray
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
.