just ME
just ME

Reputation: 1827

NSUserDefaults can't update a nsmutable array

I saved a nsmutableArray inside a NSUserDefaults.

In the following case, it seems that all the elements from the array are equal to 0, even though in this case position 1 and position 5 should have 1 instead of 0 as a value. I know that NSUserDefaults elements are immutable but ...I did add that mutableCopy when retrieving the value.

Where am I wrong?

//create array

NSMutableArray *objArray = [[NSMutableArray alloc] init];
        for (int i=0; i< 100;i++) [objArray addObject:@"0"];
        [objArray replaceObjectAtIndex:1 withObject:@"1"];
        [[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];

// update

elementPosition = 5;
 NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
        [objArray replaceObjectAtIndex:elementPosition withObject:@"1"];


//check the array


NSMutableArray *objArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:nameFile]];
    BOOL displayContent = true;


for (int i=0; i<[objArray count];i++)
{

if ([[objArray objectAtIndex:i] isEqualToString:@"0"])
{
    displayContent = false;
}
}

Upvotes: 1

Views: 586

Answers (4)

try catch finally
try catch finally

Reputation: 881

Replace your code with this:

//create array

NSMutableArray *objArray = [[NSMutableArray alloc] init];
for (int i=0; i< 100;i++){
    [objArray addObject:@"0"];
}
[objArray replaceObjectAtIndex:1 withObject:@"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];

//First mistake. Missing this. Without this line the data is not saved in NSUserDefaults
[[NSUserDefaults standardUserDefaults] synchronize];

// update

elementPosition = 5;
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:@"1"];

//Second Mistake. You did not update the NSUserDefaults again.
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
[[NSUserDefaults standardUserDefaults] synchronize];




//check the array
NSMutableArray *objArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]];
BOOL displayContent = true;


for (int i=0; i<[objArray count];i++)
{

    if ([[objArray objectAtIndex:i] isEqualToString:@"0"])
    {
        displayContent = false;
    }

   //Third mistake. Once the BOOL is made false in an if block you have to make it true in the else block, otherwise the value of the BOOL will remain false even if it does not enter the if block.
    else{
        displayContent = true;
    }
    NsLog(@"ArrayIndex=[%d];DisplayContent=[%d]",i,displayContent);
}

Happy coding.

Upvotes: 0

hoptqVN.dev
hoptqVN.dev

Reputation: 84

I think you should fix your code follow my bellow code:

[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
[[NSUserDefaults standardUserDefaults] synchronize];

...

NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:@"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:nameFile];
[[NSUserDefaults standardUserDefaults] synchronize];

Upvotes: 1

Retro
Retro

Reputation: 4005

I think when you retrieve and your mutable array from UserDefault and updated but you didn't set that new updated object to UserDefault, its having the old object which was set before. you have to store your update array again to userdefault with same key which update your UserDefautlt.

elementPosition = 5;
 NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
        [objArray replaceObjectAtIndex:elementPosition withObject:@"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];

Upvotes: 2

Suhail kalathil
Suhail kalathil

Reputation: 2693

You should call the synchronize method

[[NSUserDefaults standardUserDefaults] synchronize];

Upvotes: 1

Related Questions