NSUserDefaults 数组

NSUserDefaults Arrays

我需要两个 NSMutableArray 来包含 NSString.

这些数组是:

ListOFUserNames=[NSMutableArray array];
SituationstoName =[NSMutableArray array];

我有两组不同的字符串进入每个可变数组。

named 添加到 Listofusernames

情况已添加到 SituationstoName

 [ListOFUserNames addObject:AfterNamed];
 [SituationstoName addObject:Situation];

我正在尝试用它们各自的字符串保存 listofusernamessituationstoname

 NSUserDefaults *something = [NSUserDefaults standardUserDefaults];     
 [something setObject:ListOFUserNames forKey:@"somedata"];
 [something synchronize];

 NSUserDefaults *something2 = [NSUserDefaults standardUserDefaults];  
 [something2 setObject:SituationstoName forKey:@"somedata2"];
 [something2 synchronize];

在加载它们的方法中,没有任何结果。 该方法的代码是:

NSUserDefaults *somet = [NSUserDefaults standardUserDefaults];
NSUserDefaults *somet2 = [NSUserDefaults standardUserDefaults];

ListOFUserNames = [somet objectForKey:@"somedata"] ;
SituationstoName = [somet2 objectForKey:@"somedata2"] ;
[somet synchronize];
[somet2 synchronize];

问题是它没有加载保存在可变数组中的字符串。

//initialise array
NSMutableArray *listOFUserNames=[[NSMutableArray alloc] init];
NSMutableArray *situationstoName =[[NSMutableArray alloc] init];

//add content in the array
[listOFUserNames addObject:AfterNamed];
[situationstoName addObject:Situation];

(在存储之前还要检查您的数组是否包含对象或是否为空。)

//Store these array in `NSUserDefaults`
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:listOFUserNames forKey:@"NameData"];
[userDefaults setObject:situationstoName forKey:@"SituationData"];
[userDefaults synchronize];

//Access data where ever you want to be
//(listOFUserNames and situationstoName must be array)
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
listOFUserNames = [userDefaults objectForKey:@"NameData"] ;
situationstoName = [userDefaults objectForKey:@"SituationData"] ;