title: Error: Mutating method sent to immutable object for NSMutableArray from JSON file
body: <p>This seems to be a fairly common problem, but the solutions that I have looked at do not solve the error. I am trying to read an NSMutableArray from a JSON file.  Many of the suggestions I have seen involve using <strong>mutableCopy</strong> or <strong>[NSMutableArray arrayWithArray:]</strong> but both of these solutions do not fix the problem when using the call <strong>replaceObjectAtIndex:withObject:</strong> seen below. Please let me know if you have any advice on how to solve this problem.</p>
  
  <p><strong>EDIT:</strong> I would also like to add that the inventory list is an NSMutableArray of NSMutableArray objects.</p>
  
  <p>The exact error reads:</p>
  
  <pre><code>Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
  reason: '-[__NSCFArray replaceObjectAtIndex:withObject:]: 
  mutating method sent to immutable object'
  </code></pre>
  
  <p>I have the property defined as follows at the top of my implementation file:</p>
  
  <pre><code>NSMutableArray *inventoryData;
  </code></pre>
  
  <p>I am trying to read it from a JSON file as follows:</p>
  
  <pre><code>- (void)readJSON
  {
      //Code to get dictionary full of saves from JSON file (overworld.json) - includes the file path on the ipad as well as
      //the dictionary itself
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *localPath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"savedPaintGameData.json"]];
      NSString *filePath = [localPath mutableCopy];
      NSError *e = nil;
  
      // Read data from file saved previously - read the raw data from the path, then parse it into a dictionary using JSONObjectWithData
      NSData *RawJSON = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&amp;e];
  
      if (RawJSON == nil) {
          [self saveGameInitialize];
      } else {
          NSMutableDictionary *localDictionary = [[NSMutableDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData:RawJSON options:NSJSONReadingAllowFragments error:&amp;e]];
          NSMutableDictionary *savedDataDictionary = [localDictionary mutableCopy];
  
          //inventoryData = [[savedDataDictionary objectForKey:@"inventory"] mutableCopy];
          inventoryData = [NSMutableArray arrayWithArray:[savedDataDictionary objectForKey:@"inventory"]];
      }
  }
  </code></pre>
  
  <p>I am then trying to replace an object at the given index of the NSMutableArray as seen here:</p>
  
  <pre><code>- (void)setInventoryData: (NSString *) colorKey: (int) change
  {
      // Check if inventory already contains the paint and change the amount
      bool foundPaint = false;
      int newAmount = 100;    // Magic number prevents crashing @ removal check
      for (int i = 0; i &lt; [inventoryData count]; i++) {
          NSMutableArray *object = [inventoryData objectAtIndex:i];
          if ([[object objectAtIndex:0] isEqualToString:colorKey]) {
              newAmount = [[object objectAtIndex:1] integerValue] + change;
              [[inventoryData objectAtIndex:i] replaceObjectAtIndex:1 withObject:[NSNumber numberWithInt:newAmount]];
              foundPaint = true;
              break;
          }
      }
  
      if (newAmount == 0) {
          [self removeInventoryColor:colorKey];
      }
  }
  </code></pre>