Omer Waqas Khan Omer Waqas Khan - 2 months ago 198
Objective-C Question

Saving NSArray to NSUserDefaults and getting it in NSMutableArray

How can I save

NSArray
in
NSUserDefaults
and then load it back in an
NSMutableArray
to populate the
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
).

Any help appreciated..

Thanks

Answer
 [[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:@"Key"];

A mutable array can be stored since it is a subclass of NSArray.

To get the value as an NSMutableArray:

NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"]];

or

NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Key"] mutableCopy];
Comments