ios 将 NSArray 保存到 NSUserDefaults 并在 NSMutableArray 中获取它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9212430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving NSArray to NSUserDefaults and getting it in NSMutableArray
提问by Omer Waqas Khan
How can I save NSArray
in NSUserDefaults
and then load it back in an NSMutableArray
to populate the UIPickerView
?
我如何保存NSArray
在NSUserDefaults
,然后加载回以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
谢谢
回答by Vignesh
[[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];
回答by Jonny
// Store it
[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:@"Key"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Read it back
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"]];
回答by vakio
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 稍后需要发布,因为它是copy
.
回答by barley
Obtain NSMutableArray
instance using [[NSMutableArray alloc] initWithArray:]
after recovering NSArray
from NSUserDefaults
.
获得NSMutableArray
使用实例[[NSMutableArray alloc] initWithArray:]
恢复后NSArray
从NSUserDefaults
。
回答by Omer Waqas Khan
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];