iPhone: preserve NSUserDefaults values when applic

2019-03-02 03:18发布

问题:

I am trying to implement "Add to Favorites" functionality using NSUserDefaults. So far I have written following 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];
}
}

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?

    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;
    }

Edit:

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.

-(BOOL)isAddedToFavorites:(NSString*)viewID {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *favourites = [[standardUserDefaults objectForKey:kFavouriteItemsKey] mutableCopy];

if(favourites && [[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];
}
}

Thanks.

回答1:

NSUserDefaults is actually used to store values permanently, in fact if you create any Settings for your program they will be saved as NSUserDefaults.

I think the problem is that you are not saving it with the same key you are retrieving. Try saving like this:

  //To save
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  [defaults setObject:appDefaults forKey:kFavouriteItemsKey];
  [[NSUserDefaults standardUserDefaults] synchronize];

  //To retrieve
 NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
 NSMutableDictionary *favourites = [[standardUserDefaults objectForKey:kFavouriteItemsKey]   mutableCopy];

For the dictionary try:

 NSDictionary *myDictionary= [[NSUserDefaults standardUserDefaults] objectForKey:kFavouriteItemsKey];

        // Create a mutable dictionary to replace the old immutable dictionary
    NSMutableDictionary *myMutableDictionary= [NSMutableDictionary dictionaryWithCapacity:[myDictionary count]+1];

        // transfer the old dictionary into the new dictionary
    [myMutableDictionaryaddEntriesFromDictionary:myDictionary];

        // Now add the data
    [myMutableDictionary setObject:myObject forKey:myKey];