How can I save NSArray
in NSUserDefaults
and then load it back in an NSMutableArray
to populate the UIPickerView
?
如何在NSUserDefaults中保存NSArray,然后在NSMutableArray中加载它来填充UIPickerView?
Also the issue is that new values would be added to that NSMutableArray
and then that array will be converted to NSArray
to be saved in NSUserDefaults
(as NSMutableArray
can't be saved in NSUserDefaults
).
同样的问题是,新的值将被添加到NSMutableArray中,然后该数组将被转换为NSArray以保存在NSUserDefaults中(因为NSMutableArray不能保存在NSUserDefaults中)。
Any help appreciated..
任何帮助表示赞赏。
Thanks
谢谢
5 个解决方案
#1
106
[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:@"Key"];
A mutable array can be stored since it is a subclass of NSArray.
可变数组可以存储,因为它是NSArray的子类。
To get the value as an NSMutableArray:
想要得到一个NSMutableArray的值:
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"]];
or
或
NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"] mutableCopy];
#2
9
// Store it
[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:@"Key"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Read it back
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"]];
#3
4
To convert from NSArray to NSMutableArray:
从NSArray转换为NSMutableArray:
[array mutableCopy];
To convert from NSMutableArray to NSArray:
从NSMutableArray转换为NSArray:
[NSArray arrayWithArray:array];
Edit: mutableCopy will need a release later, since it is a copy
.
编辑:mutableCopy需要一个发行版本,因为它是一个拷贝。
#4
2
Obtain NSMutableArray
instance using [[NSMutableArray alloc] initWithArray:]
after recovering NSArray
from NSUserDefaults
.
在从NSUserDefaults中恢复NSArray之后,使用[[NSMutableArray alloc] initWithArray:]获取NSMutableArray实例。
#5
2
As this question is being viewed by many new iOS developers. I want to add more to help newbie's.
由于这个问题正在被许多新的iOS开发人员看到。我想增加一些来帮助新手。
-(void)arraysFunction:(NSArray*)array mutArray:(NSMutableArray*)mArray{
if (array == nil || [array count] == 0) {
[[NSUserDefaults standardUserDefaults] setObject:mArray forKey:@"mutableArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Mutable Array: %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"mutableArray"]);
NSArray * simpleArray = [ NSArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"mutableArray"]];
NSLog(@"Simple Array: %@", simpleArray);
}
else{
[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"array"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Simple Array: %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"array"]);
NSMutableArray * mutableArray = [NSMutableArray arrayWithArray:[[[NSUserDefaults standardUserDefaults] objectForKey:@"array"] mutableCopy]];
NSLog(@"Mutable Array: %@", mutableArray);
}
}
I hope this will help you to save NSArray
into NSUserDefaults
and then retrieving it as NSMutableArray
vice versa.
我希望这将帮助您将NSArray保存到NSUserDefaults中,然后以NSMutableArray的形式检索它,反之亦然。
You can call this method like:
你可以这样称呼这个方法:
NSArray * array = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil];
NSMutableArray * mutableArray = [[NSMutableArray alloc] initWithObjects:@"D", @"E", @"F", nil];
[self arraysFunction:array mutArray:nil];
[self arraysFunction:nil mutArray:mutableArray];
#1
106
[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:@"Key"];
A mutable array can be stored since it is a subclass of NSArray.
可变数组可以存储,因为它是NSArray的子类。
To get the value as an NSMutableArray:
想要得到一个NSMutableArray的值:
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"]];
or
或
NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"] mutableCopy];
#2
9
// Store it
[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:@"Key"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Read it back
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"]];
#3
4
To convert from NSArray to NSMutableArray:
从NSArray转换为NSMutableArray:
[array mutableCopy];
To convert from NSMutableArray to NSArray:
从NSMutableArray转换为NSArray:
[NSArray arrayWithArray:array];
Edit: mutableCopy will need a release later, since it is a copy
.
编辑:mutableCopy需要一个发行版本,因为它是一个拷贝。
#4
2
Obtain NSMutableArray
instance using [[NSMutableArray alloc] initWithArray:]
after recovering NSArray
from NSUserDefaults
.
在从NSUserDefaults中恢复NSArray之后,使用[[NSMutableArray alloc] initWithArray:]获取NSMutableArray实例。
#5
2
As this question is being viewed by many new iOS developers. I want to add more to help newbie's.
由于这个问题正在被许多新的iOS开发人员看到。我想增加一些来帮助新手。
-(void)arraysFunction:(NSArray*)array mutArray:(NSMutableArray*)mArray{
if (array == nil || [array count] == 0) {
[[NSUserDefaults standardUserDefaults] setObject:mArray forKey:@"mutableArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Mutable Array: %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"mutableArray"]);
NSArray * simpleArray = [ NSArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"mutableArray"]];
NSLog(@"Simple Array: %@", simpleArray);
}
else{
[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"array"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Simple Array: %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"array"]);
NSMutableArray * mutableArray = [NSMutableArray arrayWithArray:[[[NSUserDefaults standardUserDefaults] objectForKey:@"array"] mutableCopy]];
NSLog(@"Mutable Array: %@", mutableArray);
}
}
I hope this will help you to save NSArray
into NSUserDefaults
and then retrieving it as NSMutableArray
vice versa.
我希望这将帮助您将NSArray保存到NSUserDefaults中,然后以NSMutableArray的形式检索它,反之亦然。
You can call this method like:
你可以这样称呼这个方法:
NSArray * array = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil];
NSMutableArray * mutableArray = [[NSMutableArray alloc] initWithObjects:@"D", @"E", @"F", nil];
[self arraysFunction:array mutArray:nil];
[self arraysFunction:nil mutArray:mutableArray];