title: Mutable Arrays in Objective-C. How to change/manipulate array elements?
body: <p>I'm trying to build an array of 400 integers (treated in the context of a 20 x 20 matrix). All elements are initialized to 0, and then 40 random spots are chosen to have an integer 9 (designated as a mine in a minesweeper context). For some reason, my program hangs on the line:</p>
  
  <p><strong>[map replaceObjectAtIndex: mine_placement withObject:[NSNumber numberWithInt:9]];</strong></p>
  
  <p>Following this, I have a routine that accumulates the proximity of mines per each element, and the final bits create string objects to send out to the console (log). I'd love to hear from the Objective-C pros as to what I'm doing wrong, and why this isn't outputting correctly. It builds fine, just no output. I get some message about threads being started. I'm new to Obj-C, so any thoughts on the matter would be awesome.</p>
  
  <p>Here's the contents inside main:</p>
  
  <pre><code>@autoreleasepool
  {
  
      int number_of_mines = 40;
      int mine_placement;
  
      // create map of 400 char elements
      NSMutableArray* map = [[NSMutableArray alloc] init];
  
      // char map[400];
      // build array with zeros everywhere
      for(int i = 0; i &lt; 400; i++)
          [map addObject:[NSNumber numberWithInt:0]];
  
      // add randomly placed mines
  
      for(int i = 0; i &lt; number_of_mines; i++) // loop for 40 mines
      {
          mine_placement = arc4random() % 400;
          while([map objectAtIndex: mine_placement] == [NSNumber numberWithInt:9]) // if we are already on a mine, repeat
              {
                  mine_placement = arc4random() % 400;
              }
          [map replaceObjectAtIndex: mine_placement withObject:[NSNumber numberWithInt:9]];
      }
  
      // proximity accumulator (an iterative approach)
      for(int i = 0; i &lt; 400; i++)
      {
          if([map objectAtIndex: i] != [NSNumber numberWithInt:9])
          {
              int accumulator = 0;
  
              // check top three neighbors
              if(i &gt;= 20) // past first row
              {
                  if(i % 20 != 0) // past first column
                      if([map objectAtIndex: (i-21)] == [NSNumber numberWithInt:9]) // top-left
                          accumulator++;
  
                  if((i+1) % 20 != 0) // before last column
                      if([map objectAtIndex: (i-19)] == [NSNumber numberWithInt:9]) //top-right
                              accumulator++;
  
                  if([map objectAtIndex: (i-20)] == [NSNumber numberWithInt:9]) // top
                      accumulator++;
              }
  
              // check bottom three neighbors
              if(i &lt; 380) // before last row
              {
                  if(i % 20 != 0) // past first column
                      if([map objectAtIndex: (i+19)] == [NSNumber numberWithInt:9]) // bottom-left
                          accumulator++;
  
                  if((i+1) % 20 != 0) // before last column
                      if([map objectAtIndex: (i+21)] == [NSNumber numberWithInt:9]) // bottom-right
                              accumulator++;
  
                  if([map objectAtIndex: (i+20)] == [NSNumber numberWithInt:9]) // bottom
                      accumulator++;
              }
  
              // left neighbor
              if(i % 20 != 0) // past first column
                  if([map objectAtIndex: (i-1)] == [NSNumber numberWithInt:9]) // left
                      accumulator++;
  
              // right neighbor
              if((i+1) % 20 != 0) // before last column
                  if([map objectAtIndex: (i+1)] == [NSNumber numberWithInt:9]) // right
                      accumulator++;
  
              if(accumulator &gt; 0)
                  [[map objectAtIndex: i] replaceObjectAtIndex: i withObject:[NSNumber numberWithInt:accumulator]];
          }
      }
  
  
      // output map of 400 char elements
      // build string rows
  
      NSMutableString* string_row = [[NSMutableString alloc] init];
      for(int row = 0; row &lt; 20; row++)
      {
          for(int s = 0; s &lt; 20; s++)
          {
              [string_row insertString:[map objectAtIndex: s] atIndex:s];
          }
          NSLog(@"%@", string_row);
          [string_row setString:@""]; // clear string_row for next row pass
      }
  }
  </code></pre>