title: Creating an Array of Arrays in XCode
body: <p>I am trying to build an array that contains arrays of dictionary objects in Xcode.  I can create a working array that has one or more dictionaries in it and then use the addObject: method to add this array as an object in my main array.  I cycle around this several times, rebuilding the working array and adding objects to the main array.  All good so far, except that on inspecting my main array it contains duplicates of the last dictionaries that the working array was built with.  I am assuming this is because when I use the addObject it simply assigns a pointer to the array but does not increase the retain count or create a copy of the working array, hence every time it is rebuilt the main array simply points to the this array.</p>
  
  <p>I guess my question is how do I create a copy of the working array add it to the main array and then rebuild it and add it again?  Hope that makes sense and sorry if this appears to be a basic question - it is (I'm new to Xcode) - and any help would be really appreciated.</p>
  
  <hr>
  
  <pre><code>        [sectionArray release];
          sectionArray  = [[NSMutableArray alloc] init];
          [sectionArray addObject:dict];
  </code></pre>
  
  <p>This all works, which is great. One thing I don't have my head around yet is, is there a danger that when I am done with the main array and I do a [mainArray release] too that these child arrays will be left behind in memory or will the release destroy them as well?</p>
  
  <hr>
  
  <p>I have solved it by re-initialising the inner working array instead of just removing all objects, i.e.,</p>
  
  <p>I did have:</p>
  
  <pre><code>        [sectionArray removeAllObjects];
          [sectionArray addObject:dict];
  </code></pre>
  
  <p>And changed it to:</p>
  
  <pre><code>        sectionArray  = [[NSMutableArray alloc] init];
          [sectionArray addObject:dict];
  </code></pre>
  
  <p>I have a feeling though that this is not a good thing to do and I will start to leak memory all over the place.  </p>