Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because your instance variables <code>self.arr1</code>, <code>self.arr2</code> are placeholders for a variable of type <code>NSArray</code>, but when you update them you just lose the old reference that still remains in the <code>containerArray</code>.</p> <p>To explain better:</p> <pre><code>self.arr1 &lt;------ [NSArray array] (the first one you create) </code></pre> <p>then you build up the <code>contentArray</code> and you have:</p> <pre><code>self.arr1 &lt;--------------------------+-- [NSArray array] (first one you create) | [contentArray objectAtIndex:0] &lt;-----+ </code></pre> <p>after you create your <code>contentArray</code> and you assign the arrays to it you have duplicated references. <code>self.arr1</code> will point to the same array pointed by first element of <code>contentArray</code> and so on for others.</p> <p>Then you update <code>self.arr1 = [NSArray array]</code> with a new array so what you obtain is</p> <pre><code>self.arr1 &lt;------------ [NSArray array] (new one created) [contentArray objectAtIndex:0] &lt;----- [NSArray array] (the old one) </code></pre> <p>So what's happened?</p> <p>You created a new array and assigned it to <code>self.arr1</code>. So <code>self.arr1</code> will point to the new item while <code>[contentArray objectAtIndex:0]</code> will still reference to the old one. Every modification to <code>self.arr1</code> will be not reflected on the other one because they are <strong>NOT</strong> the same object.</p> <p>This because you are using <em>references</em> to objects and not plain objects. When you assign a new array to <code>self.arr1</code> you are not modifying the old one but discarding the reference to it for a new one that replaces the old one.</p> <p>That's why you should use a <code>NSMutableArray</code>: because in that case you would remove/add elements to the same array, without creating a new one.</p>
 

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