title: iPhone: preserve NSUserDefaults values when application is killed
body: <p>I am trying to implement "Add to Favorites" functionality using NSUserDefaults. So far I have written following code.</p>
  
  <pre><code>- (void)favouriteButtonClicked:(id)sender
  {
  favselected = !favselected; // favselected is BOOL property
  MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  NSString* viewname = @"custom";
  
  if(favselected) {
      [favButton setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
      [appDelegate addTOFavourites:self.ViewID viewType:self.ViewType];
  } else {
      [favButton setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
      [appDelegate removeFromFavourites:self.ViewID viewType:self.ViewType];
  }
  }
  </code></pre>
  
  <p>It is working fine as long as my application is running but when I killed my application, I am losing all my stored values so when next time view loaded, in viewload isAddedToFavorites method returns false. Is there anyway to preserve my values? Am I missing something?</p>
  
  <pre><code>    if([appDelegate isAddedToFavorites:self.ViewID]) {
          [favButton setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
          favselected = YES;
      } else {
          [favButton setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
          favselected = NO;
      }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>I tried using NSMutableDictionary as I have to add multiple key-values but following method always display Count=0 even after adding object to dictionary. Any help would be really appreciated. Thank you.</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 {
  
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  NSMutableDictionary *dict = [[standardUserDefaults objectForKey:kFavouriteItemsKey] mutableCopy];
  
  if(standardUserDefaults) {
      if(![dict objectForKey:viewID])
          [dict setObject:viewType forKey:viewID]; // It is coming here but still count zero!
  
      NSLog(@"count = %d", [dict count]);
  
      [standardUserDefaults setObject:dict  forKey:kFavouriteItemsKey]; // Always dict remains null with 0 objects in it
      [standardUserDefaults synchronize];
      [dict release];
  }
  }
  
  -(void)removeFromFavourites:(NSString*)viewID viewType:(NSString*)viewType {
  
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  NSMutableDictionary *dict = [[standardUserDefaults objectForKey:kFavouriteItemsKey] mutableCopy];
  
  if(standardUserDefaults) {
      if ([dict objectForKey:viewID])
          [dict removeObjectForKey:viewID];
  
      [standardUserDefaults setObject:dict  forKey:kFavouriteItemsKey];
      [standardUserDefaults synchronize];
      [dict release];
  }
  }
  </code></pre>
  
  <p>Thanks.</p>