NSUserDefaults无法更新nsmutablearray

时间:2014-06-30 08:57:23

标签: ios nsmutablearray nsuserdefaults

我在NSUserDefaults中保存了一个nsmutableArray。

在下面的例子中,似乎数组中的所有元素都等于0,即使在这种情况下,位置1和位置5应该有1而不是0作为值。我知道NSUserDefaults元素是不可变的但是...我在检索值时添加了mutableCopy。

我哪里错了?

//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;
}
}

4 个答案:

答案 0 :(得分:2)

我认为当你从UserDefault检索并修改你的可变数组并更新但你没有将新的更新对象设置为UserDefault时,它具有之前设置的旧对象。您必须使用更新UserDefautlt的相同密钥将更新阵列再次存储到userdefault。

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

答案 1 :(得分:1)

您应该调用同步方法

[[NSUserDefaults standardUserDefaults] synchronize];

答案 2 :(得分:1)

我认为您应该按照我的下面的代码修改您的代码:

[[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];

答案 3 :(得分:0)

用以下代码替换您的代码:

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

快乐的编码。