Objective C - UserDefaults Over Writing

时间:2012-05-19 00:53:57

标签: iphone objective-c ios cocoa-touch ios5

我似乎无法在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");
}

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

NSUserDefaults在您对其进行更改时不会自动保存。尝试在您上面发布的内容下面添加此行:

[userDefaults synchronize];

这应该保存您所做的任何更改。

作为旁注,我不确定你能否将'nil'保存到NSUserDefaults中;在它的核心,我相信它是一个字典,其中无法设置零值。因此,您可以尝试设置一个空对象。

希望这有帮助,如果您需要更多信息,请与我们联系!