如何在 NSUserDefaults 中存储多个字典

How to Store multiple dictionary in NSUserDefaults

我在 FirstVC 中有两个 Viewcontroller,我构建了 5 UITextField 用于注册,这个 TextField 值存储在字典中,最终字典存储在 NSUserdefault 中,然后在 SecondVC 中我想显示这个数据 我的问题是每次我在 NSUserdefault 中添加新的自由裁量权时旧的字典被替换 我要所有字典的数据。

下面是我的FirstVC的代码

  -(void)btnReg

{ 
//data add in disctionary
    for (int i=1; i<=5; i++)
    {
        UITextField *txtTemp=(UITextField *)[self.view viewWithTag:i];
        [discRege setObject:[NSNumber numberWithInt:count] forKey:@"no"];
        [discRege setObject:txtTemp.text forKey:[arraylblName objectAtIndex:i-1]];
    }
//dictionary add in nsuserdefault

    [[NSUserDefaults standardUserDefaults]setObject:discRege forKey:@"ABC"];
     [[NSUserDefaults standardUserDefaults]synchronize];

//push to SecondVc
    secondViewController *objSec=[[secondViewController alloc]init];
    [self.navigationController pushViewController:objSec animated:YES];
    [self.navigationController setNavigationBarHidden:false];

}

下面是我SecondVC的代码

ArratTemp  =[[NSUserDefaults standardUserDefaults]objectForKey:@"ABC"] ;
    if (!ArratTemp )
    {
        ArratTemp =[[NSMutableArray alloc]init];
    }
    else
    {
        ArratTemp = [[[NSUserDefaults standardUserDefaults]objectForKey:@"ABC"]mutableCopy];
    }
    NSLog(@"%@",ArratTemp);

每次您使用相同的键并替换现有的字典对象时...

// Using the same key will overwrite the last saved dictionary.
[[NSUserDefaults standardUserDefaults] setObject:discRege forKey:@"ABC"];

与其将其存储为字典,不如将其存储为字典数组。每当您添加新注册时,获取保存的数组,将新的字典对象添加到其中并使用该数组更新 userDefaults。

mutableArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"ABC"] mutableCopy];
[mutableArray addObject:discReg];
[[NSUserDefaults standardUserDefaults] setObject:mutableArraay forKey:@"ABC"];
[[NSUserDefaults standardUserDefaults] synchronize];

希望对您有所帮助。

你每次都在覆盖同一个字典。

您有两个解决方案:

解决方案 1.

将不同的字典存储在不同的键下,而不是全部在"ABC"下。所以在你的 for loop 中可以使用索引 (i) 来有多个条目,而不是每次都只有 ABC。这是您可以自己解决的简单问题。确保不要将所有内容都存储在同一个键下,您会找到它们;)例如,您可以保存在 [NSNumber numberWithInt:i] 下,然后浏览 NSUserDefaults 以查找 0、1、2、3... 等等.顺便说一句,我建议不要这样做,解决方案 2 是可行的方法。

解决方案 2.

将所有字典存储在一个数组中,然后将该数组存储在 NSUserDefaults 中。

为此,只需创建一个您保留为空的 NSMutableArray,然后在其中添加字典!

NSMutableArray dataArray = [[NSMutableArray alloc]init];

for (int i=1; i<=5; i++)
    {
        //Creating new dictionary
        NSMutableDictionary *currentDict = [[NSMutableDictionary alloc]init];
        //Getting the text we want
        UITextField *txtTemp =(UITextField *)[self.view viewWithTag:i];
        NSString *text = txtTemp.text;

        //This is here because you had it
        [currentDict setObject:[NSNumber numberWithInt:count] forKey:@"no"];

        //All dictionaries will have key = name of the Label,
        //but you could change it to something static, like
        // "Content" for example. It'll be easier to find later
        [currentDict setObject:text forKey:[arraylblName objectAtIndex:i-1]];

        //Adding that newly formed dictionary to the mutable array.
        [dataArray addObject:currentDict];
    }

  //Adding the array containing dictionaries to the NSUSerDefaults
 [[NSUserDefaults standardUserDefaults]setObject:dataArray forKey:@"ABC"];

注意:我不确定您在 for 循环中使用字典做什么,但由于您没有显示代码,我猜它不是问题的一部分。有了我的回答,您就有了足够的信息,可以根据需要进行一些更正。您只需要记住:

  1. 为每个答案创建一个字典,而不是为所有答案创建一个字典
  2. 将每个字典放在同一个数组中
  3. 保存数组(包含所有字典)