title: iPhone: Not able to store NSMutableDictionary to NSUserDefaults
body: <p>I am trying to implemen "Add to Favorites" functionality using NSMutableDictionary as I have to add multiple key-values. The following addToFavourites method always display Count=0 even after adding object to dictionary. I read <a href="https://stackoverflow.com/questions/4690290/how-to-store-nsmutablearray-in-nsuserdefaults">this</a> and getting nsdefaults into mutabledictionary(instead of mutable array) and setting resulted dictionary back to nsdefaults but not sure what's the problem. Any help would be really appreciated. Thank you.</p>
  
  <p>Edited:
     From <a href="https://stackoverflow.com/questions/6585200/iphone-preserve-nsuserdefaults-values-when-application-is-killed/6585565#6585565">this</a> I came to know that I have to move data around mutable and immutable dictionary and then it worked fine! but still not able to synchronize modified NSMutableDictionary to NSUserDefaults.</p>
  
  <pre><code>-(BOOL)isAddedToFavorites:(NSString*)viewID {
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  NSMutableDictionary *favourites = [[standardUserDefaults objectForKey:kFavouriteItemsKey] mutableCopy];
  
  if(favourites &amp;&amp; [[favourites objectForKey:kFavouriteItemsKey] objectForKey:viewID])
  return YES;
  
  return NO;
  }
  
  -(void)addToFavourites:(NSString*)viewID viewType:(NSString*)viewType {
  NSMutableDictionary *myMutableDictionary= [NSMutableDictionary dictionaryWithCapacity:[myDictionary count]+1];
  [myMutableDictionary addEntriesFromDictionary:myDictionary];
  [myMutableDictionary setObject:myObject forKey:myKey];
  
  
  // Modified dictionary is not getting synced
  [[NSUserDefaults standardUserDefaults]  setObject:myMutableDictionary  forKey:kFavouriteItemsKey];
  [[NSUserDefaults standardUserDefaults]  synchronize];
  }
  </code></pre>
  
  <p>Tried encoding dictionary data to NSData and it worked now after minor but serious corrections!</p>
  
  <pre><code>-(BOOL)isAddedToFavorites:(NSString*)viewID {
  
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  NSData *data = [defaults objectForKey:kFavouriteItemsKey];
  NSDictionary *favourites = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
  
  if(favourites &amp;&amp; [favourites objectForKey:viewID])
      return YES;
  
  return NO;
  }
  
  -(void)addToFavourites:(NSString*)viewID viewType:(NSString*)viewType {
  
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  NSData *data = [defaults objectForKey:kFavouriteItemsKey];
  NSDictionary *myDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
  
  NSMutableDictionary *myMutableDictionary= [NSMutableDictionary dictionaryWithCapacity:[myDictionary count]+1];
  [myMutableDictionary addEntriesFromDictionary:myDictionary];
  [myMutableDictionary setObject:viewType forKey:viewID];
  
  
  NSData *newdata = [NSKeyedArchiver archivedDataWithRootObject:myMutableDictionary];
  [[NSUserDefaults standardUserDefaults] setObject:newdata forKey:kFavouriteItemsKey];
  [[NSUserDefaults standardUserDefaults] synchronize];
  }
  </code></pre>
  
  <p>Thanks.</p>