title: NSArray to NSMutableArray as random stack
body: <p>Just a conceptual description first:</p>
  
  <p>I am reading input from a text file (a list of words) and putting these words into an <code>NSArray</code> using <code>componentsSeparatedByString</code> method.  This works.</p>
  
  <p>But I wanted to select the words randomly and then delete them from the array so as to ensure a different word each time.  Of course, you cannot change the <code>NSArray</code> contents.  So...</p>
  
  <p>I copied the contents of the <code>NSArray</code> into an <code>NSMutableArray</code> and use IT for the selection source.  This also works - 269 objects in each array.</p>
  
  <p>To return a word from the <code>NSMutableArray</code> I use the following code:
  note- the arrays are declared globally
      as</p>
  
  <pre><code>arrMutTextWords = [[NSMutableArray alloc] init];  //stack for words 
  arrTextWords = [[NSArray alloc] init];  //permanent store for words
  
  
  -(NSString*) getaTextWord
  {
  
  // if the mutable text word array is empty refill
  
  if ([arrMutTextWords count] == 0){
  
      for (int i = 0 ; i &lt; [arrTextWords count]; i++) 
          [arrMutTextWords addObject:[arrTextWords objectAtIndex:i]];
  }
  
  int i = random() % [arrMutTextWords count];
  NSString* ptrWord = [arrMutTextWords objectAtIndex:i];
  [arrMutTextWords removeObjectAtIndex:i];
  return ptrWord;
  </code></pre>
  
  <p>}</p>
  
  <p>The program crashes during a call to the method above - here is the calling code:
  arrTmp is declared globally arrTmp = [[NSArray alloc] init];  //tmp  store for words</p>
  
  <pre><code>for (int i = 0 ; i &lt; 4; i++) {
      tmpWord = [self getaTextWord];
      [arrTmp addObject:tmpWord];
      [arrTmp addObject:tmpWord];
  }
  </code></pre>
  
  <p>I'm thinking that somehow deleting strings from <code>arrMutTextWords</code> is invalidating the <code>NSArray</code> - but I can't think how this would occur.</p>