I can’t seem to re edit this object in my ios User Defaults. Here is my code…
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:nil forKey:@"savedScoreCards2"];
Before I did this I set the key savedScoreCards to a huge 3-d dictionary, and now when I try to over write it or even set the key to nil that huge 3-d dictionary that I set up a while ago is still there. I can’t seem to over write/ replace it will nothing (nil), or another huge 3-d dictionary. Any ideas?
UPDATE:
+ (void)saveCurrentScoreCard
{
//save score card
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *savedScoreCards = [[NSMutableDictionary alloc] init];
[userDefaults setObject:nil forKey:@"savedScoreCards2"];//reset at beginning
NSMutableArray *golferOneScoresu = [userDefaults arrayForKey:@"golferOneScoresArray"];
NSMutableArray *golferTwoScoresu = [userDefaults arrayForKey:@"golferTwoScoresArray"];
NSMutableDictionary *golferIconsu = [userDefaults objectForKey:@"golferIconsFirstScene"];
NSMutableArray *golferThreeScoresu = [userDefaults arrayForKey:@"golferThreeScoresArray"];
NSMutableArray *golferFourScoresu = [userDefaults arrayForKey:@"golferFourScoresArray"];
NSMutableDictionary *golferIcons2u = [userDefaults objectForKey:@"golferIcons"];
NSMutableArray *golferNamesu = [userDefaults objectForKey:@"golferNames"];
NSMutableArray *golferNames = [[NSMutableArray alloc] initWithArray:golferNamesu copyItems:YES];
NSMutableDictionary *golferIconsSceneOne = [[NSMutableDictionary alloc]initWithDictionary:golferIconsu];
NSMutableDictionary *golferIcons = [[NSMutableDictionary alloc]initWithDictionary:golferIcons2u];
NSMutableArray *golferFourScores = [[NSMutableArray alloc] initWithArray:golferFourScoresu copyItems:YES];
NSMutableArray *golferThreeScores = [[NSMutableArray alloc] initWithArray:golferThreeScoresu copyItems:YES];
NSMutableArray *golferOneScores = [[NSMutableArray alloc] initWithArray:golferOneScoresu copyItems:YES];
NSMutableArray *golferTwoScores = [[NSMutableArray alloc] initWithArray:golferTwoScoresu copyItems:YES];
NSMutableDictionary *currentScoreCard = [[NSMutableDictionary alloc] init];
[currentScoreCard setObject:golferNames forKey:@"golferNames"];
[currentScoreCard setObject:golferIconsSceneOne forKey:@"golferIconsFirstScene"];
[currentScoreCard setObject:golferIcons forKey:@"golferIcons"];
[currentScoreCard setObject:golferOneScores forKey:@"golferOneScoresArray"];
[currentScoreCard setObject:golferTwoScores forKey:@"golferTwoScoresArray"];
[currentScoreCard setObject:golferThreeScores forKey:@"golferThreeScoresArray"];
[currentScoreCard setObject:golferFourScores forKey:@"golferFourScoresArray"];
[savedScoreCards setObject:currentScoreCard forKey:@"1"];
NSLog([golferIconsSceneOne objectForKey:@"30101"]);
[userDefaults setObject:savedScoreCards forKey:@"savedScoreCards2"];
[userDefaults synchronize];
NSLog(@"SAVED SCORE CARD");
}
Use
[userDefaults removeObjectForKey:@"savedScoreCards2"]if you’re just trying to remove a stored value.Also call
[userDefaults synchronize];to “flush” your changes immediately. I think the[userDefaults synchronize]is probably what you’re missing.