How to update NSUserDefault

I have the following code that should update a value in 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;

}

As far as I can tell, this is a local code. This is probably "more verbose" than it should be. But, as I understand it, everything that came back from NSUserDefaults will be immutable, so I have to recreate it as a Mutable object, add what I need to add, and then replace the object in NSUserDefaults. And I think that what I'm trying to do above should work.

output for NSLog (@ "New boards:% @", 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;
}

"String = test" is just a test entry and is set in the AppDelegate.m file as follows:

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

So, I know that what I need definitely exists. I just know that it will be something stupid that I am missing. But I do not see this or do not understand.

- , ?

+3
3

, . , , "", . , , , NSUserDefaults, , Mutable, , , NSUserDefaults.

- mutableCopy :

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

, synchronize . , , plist Finder. NSUserDefaults . , , , - synchronize, , .

+5

:

, ( - ): NSData, NSString, NSNumber, NSDate, NSArray NSDictionary.

, , ? NSKeyedArchiver

NSUserDefaults NSMutableDictionary iPhone SDK?

0

:

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

, :

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

[[NSUserDefaults standardUserDefaults] ] - . . IOS .

. . . , .

0

Source: https://habr.com/ru/post/1725487/


All Articles