代码之家  ›  专栏  ›  技术社区  ›  gargantuan

如何更新NSUserDefault

  •  3
  • gargantuan  · 技术社区  · 15 年前

    我有以下代码,可以更新NSUserDefaults中的值:

    - (id) createBoardWithTitle:(NSString *)pTitle withScores:(NSArray *)pScores andNames:(NSArray *)pNames andDisplayStrings:(NSArray *)pStrings orderBy:(NSString *)pOrder ofType:(NSString *)pType
    {
    
        if((self == [super init]))
        {
    
            boardEntries = [NSMutableArray arrayWithCapacity:10];
    
            // Build the data
    
                for(...){
                   // populate boardEntries
                }
    
    
            // create an Dictionary to save
            NSDictionary *savedData = [NSDictionary dictionaryWithObjectsAndKeys:pType, @"type", pOrder, @"order", boardEntries, @"entries", nil];
    
                // Load the old boards
            NSDictionary *existingBoards = [[NSUserDefaults standardUserDefaults] objectForKey:@"BlockDepotLeaderboards"];
    
                // Create a mutable dictionary to replace the old immutable dictionary
            NSMutableDictionary *newBoards = [NSMutableDictionary dictionaryWithCapacity:[existingBoards count]+1];
    
                // transfer the old dictionary into the new dictionary
            [newBoards addEntriesFromDictionary:existingBoards];
    
                // add the new board to the new dictionary
            [newBoards setObject:savedData forKey:pTitle];
    
                // check to make sure it looks like it should by eye
            NSLog(@"New Boards: %@", newBoards);
    
                // Replace the old date in NSUserdefaults
            [[NSUserDefaults standardUserDefaults] setObject:newBoards forKey:@"BlockDepotleaderboards"];
                // Update: Do I really need to call this?
            [[NSUserDefaults standardUserDefaults] synchronize];
    
                // Check to make sure it looks as it should by eye
            NSLog(@" Defaults--- %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"BlockDepotLeaderboards"]);
        }
    
        return self;
    
    }
    

    NSLog(@“新板:%@”,newBoards)id的输出

    New Boards: {
        "Marathon: Times" =     {
            entries =         (
                            {
                    displayString = "10:00";
                    name = "Tom Elders";
                    score = 600;
                },
                            {
                    displayString = "10:30";
                    name = "A.R. Elders";
                    score = 630;
                },
                            {
                    displayString = "11:00";
                    name = "L. Lancaster-Harm";
                    score = 660;
                },
    
                // and so on.....
    
            );
            order = ASC;
            type = TIMES;
        };
        String = Test;
    }
    

    if(![[NSUserDefaults standardUserDefaults] objectForKey:@"BlockDepotLeaderboards"]){
    
        NSMutableDictionary *leadboardHolder = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Test", @"String", nil];      
        [[NSUserDefaults standardUserDefaults] setObject:leadboardHolder forKey:@"BlockDepotLeaderboards"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    
    }else{
        NSLog(@"Leaderboards Dict Exists %@", [NSUserDefaults standardUserDefaults]);
    }
    

    所以我知道我所追求的肯定是存在的。我只知道我会错过一些愚蠢的事情。但我看不见也弄不明白。

    3 回复  |  直到 15 年前
        1
  •  5
  •   Adrian    15 年前

    据我所知,这是相关的代码。它可能比它需要的更“冗长”。但据我所知,从NSUserDefaults返回的任何内容都是不可变的,因此我必须将其重新创建为可变对象,添加需要添加的内容,然后在NSUserDefaults中替换该对象。

    更简单的方法是制作不可变字典的可变副本,如下所示:

    NSMutableDictionary *newBoards = [[immutableDict mutableCopy] autorelease];
    [newBoards setObject:savedData forKey:pTitle];
    

    也, synchronize 用于强制将用户默认值保存为磁盘。它只会影响在Finder中打开plist时看到的内容。从应用程序的角度来看 NSUserDefaults 总是最新的。此外,默认值每隔一段时间就会自动保存,因此您可能只需要调用 使同步 是应用程序终止的时间。

        2
  •  0
  •   Community CDub    7 年前

    根据文件:

    列表,即(或的)实例 集合是实例的组合 of):NSData、NSString、NSNumber、, NSDate、NSArray或NSDictionary。

    您确定要序列化的所有对象吗?看看 NSKeyedArchiver

    Why NSUserDefaults failed to save NSMutableDictionary in iPhone SDK?

        3
  •  0
  •   Eric Aya    7 年前

    首先保存任何内容:

    [[NSUserDefaults standardUserDefaults] setObject:@"Previous Value" forKey:@"Name"];
    

    [[NSUserDefaults standardUserDefaults] setObject:@"Updated Value" forKey:@"Name"];
    

    您不需要调用[[NSUserDefaults standardUserDefaults] 管理得很好。

    您不需要删除对象。只需更新相同的密钥。它将更新该特定键的值。 希望这有帮助。