title: Deleting key from NSDictionary
body: <p>In my app I am storing a list of sets of data in NSUserDefaults. When it needs to be accessed,  I create create an array (each key in NSUserDefaults contains and array of data) and use the data. It works great. Yay! Unfortunately, when I display the data in a UITableView, deleting one set of data ( which == one row in the UITableView ) is not working properly. This is how I am going about doing that:</p>
  
  <pre><code>if(buttonIndex == actionSheet.destructiveButtonIndex)
  {
      NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  
      int iii = [defaults integerForKey:@"currentMap"];
  
      NSMutableDictionary* dicOne = (NSMutableDictionary*)[[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
  
      for(int lll = iii; lll &lt; [[defaults objectForKey:@"counterKey"] intValue]; lll++)
      {
          if(lll != [[defaults objectForKey:@"counterKey"] intValue] - 1)
              [dicOne setObject:[dicOne objectForKey:[NSString stringWithFormat:@"%i", (iii + 1)]] forKey:[NSString stringWithFormat:@"%i", iii]];
  
          else
              [dicOne removeObjectForKey:[NSString stringWithFormat:@"%i", iii]];
      }
  
      [defaults setPersistentDomain:dicOne forName:[[NSBundle mainBundle] bundleIdentifier]];
  
      [defaults synchronize];
  
      int counter = [defaults integerForKey:@"counterKey"];
  
      counter--;
  
      [defaults setInteger:counter forKey:@"counterKey"];
  
      [defaults synchronize];
  
      [self performSelector:@selector(done:) withObject:nil afterDelay:.3];
  }
  </code></pre>
  
  <p>The idea behind this code is to start a for loop at the index of the selected row to be deleted ( variable 'iii'). This method moves through the dictionary, moving each index down a level. </p>
  
  <p>Imagine the dictionary containing five sets of data (five keys), labeled 0, 1, 2, 3, and 4. When 2 gets marked for deletion, this code moves through the dictionary, assigning the data contained in key 3 to key 2, the data in key 4 to key 3, then deletes the remaining copy in key three. The counterKey then gets decremented (as this is the number I use to keep track of how many keys exist, so I can tell the UITableView how many cells it needs to create). Or at least that is what I think it <em>should</em> do. </p>
  
  <p>But it doesn't do what I think it should do; when a cell gets marked for deletion, what <em>really</em> happens is that the data for that key shows up as NULL, and the keys do not 'slide' like I think they should. </p>
  
  <p>The done: method dismisses the information view (that contains the delete button and information about the selected row) and returns to the view that holds the UITableView. </p>
  
  <p>Does this concept make sense, and if so, why doesn't it work? Thanks for your time.</p>
  
  <p>***Edit:</p>
  
  <p>Thank you, jrturton, using an NSArray worked, but sometimes deleting it (or trying) crashes with this error:</p>
  
  <p><strong>* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
  *</strong> First throw call stack:
  (0x14c7052 0x1658d0a 0x146fa78 0x146f9e9 0x14c109f 0xd381 0x8cba1f 0x14c8ec9 0x5105c2 0x51055a 0x5b5b76 0x5b603f 0x5b52fe 0x535a30 0x535c56 0x51c384 0x50faa9 0x24adfa9 0x149b1c5 0x1400022 0x13fe90a 0x13fddb4 0x13fdccb 0x24ac879 0x24ac93e 0x50da9b 0x1ef9 0x1e75)</p>
  
  <p>Which is odd, because there is not a single NSArray used in that class; all of them are mutable.</p>
  
  <p>This is the line of the crash (I am reasonably sure):</p>
  
  <pre><code>NSMutableArray* outerArray = (NSMutableArray*)[defaults objectForKey:@"mapSaveDataKey"];
  [outerArray removeObjectAtIndex:iii];
  </code></pre>
  
  <p><strong>Fix:</strong></p>
  
  <p>Replace:</p>
  
  <pre><code>[defaults objectForKey:@"mapSaveDataKey"]; 
  </code></pre>
  
  <p>With:</p>
  
  <pre><code>[defaults mutableArrayValueForKey:@"mapSaveDataKey"];
  </code></pre>