0

我似乎无法在我的 ios 用户默认值中重新编辑此对象。这是我的代码...

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:nil forKey:@"savedScoreCards2"];

在我这样做之前,我将键 savedScoreCards 设置为一个巨大的 3-d 字典,现在当我尝试重写它甚至将键设置为 nil 时,我不久前设置的那个巨大的 3-d 字典仍然存在。我似乎无法过度写入/替​​换它(零)或另一个巨大的 3-d 字典。有任何想法吗?

更新:

  + (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");
}
4

2 回答 2

3

如果[userDefaults removeObjectForKey:@"savedScoreCards2"]您只是想删除存储的值,请使用。还请立即致电[userDefaults synchronize];“刷新”您的更改。我认为这[userDefaults synchronize]可能是你所缺少的。

于 2012-05-19T00:57:58.423 回答
1

当您对 NSUserDefaults 进行更改时,它们不会自动保存。尝试在您上面发布的行下方添加此行:

[userDefaults synchronize];

This should save any changes you've made.

As a side note, I'm not sure you can save 'nil' into NSUserDefaults; at its core I believe it's a dictionary, in which nil values cannot be set. So you might try setting an empty object, instead.

Hope this helps, let me know if you need any more information!

于 2012-05-19T00:58:26.530 回答