Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>copy</code> and <code>mutableCopy</code> are defined in different protocols (<code>NSCopying</code> and <code>NSMutableCopying</code>, respectively), and <code>NSArray</code> conforms to both. <code>mutableCopy</code> is defined for <code>NSArray</code> (not just <code>NSMutableArray</code>) and allows you to make a mutable copy of an originally immutable array:</p> <pre><code>// 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"]; </code></pre> <p>Summary: </p> <ul> <li>you can depend on the result of <code>mutableCopy</code> to be mutable, regardless of the original type. In the case of arrays, the result should be an <code>NSMutableArray</code>.</li> <li>you <em>cannot</em> depend on the result of <code>copy</code> to be mutable! <code>copy</code>ing an <code>NSMutableArray</code> <em>may</em> return an <code>NSMutableArray</code>, since that's the original class, but <code>copy</code>ing any arbitrary <code>NSArray</code> instance would not.</li> </ul> <p><strong>Edit:</strong> 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. <code>copy</code> vs <code>mutableCopy</code> affects whether the <em>new array</em> is mutable.</p> <p><strong>Edit 2:</strong> Fixed my (false) assumption that <code>NSMutableArray -copy</code> would return an <code>NSMutableArray</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload