Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save NSMutablearray in NSUserDefaults

I have two NSMutableArray's. They consist of images or text. The arrays are displayed via a UITableView. When I kill the app the data within the UITableView gets lost. How to save array in UITableView by using NSUserDefault?

like image 904
user2841148 Avatar asked Oct 28 '13 12:10

user2841148


People also ask

How do you save NSUserDefaults in Objective C?

static func setObject(value:AnyObject ,key:String) { let pref = NSUserDefaults. standardUserDefaults() pref. setObject(value, forKey: key) pref. synchronize() } static func getObject(key:String) -> AnyObject { let pref = NSUserDefaults.

What types can you store natively in NSUserDefaults?

Storing Default Objects The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs. These methods are described in Setting Default Values.

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

What is NSMutableArray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray .


2 Answers

Note: NSUserDefaults will always return an immutable version of the object you pass in.

To store the information:

// Get the standardUserDefaults object, store your UITableView data array against a key, synchronize the defaults NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:arrayOfImage forKey:@"tableViewDataImage"]; [userDefaults setObject:arrayOfText forKey:@"tableViewDataText"]; [userDefaults synchronize]; 

To retrieve the information:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSArray *arrayOfImages = [userDefaults objectForKey:@"tableViewDataImage"]; NSArray *arrayOfText = [userDefaults objectForKey:@"tableViewDataText"]; // Use 'yourArray' to repopulate your UITableView 

On first load, check whether the result that comes back from NSUserDefaults is nil, if it is, you need to create your data, otherwise load the data from NSUserDefaults and your UITableView will maintain state.

Update

In Swift-3, the following approach can be used:

let userDefaults = UserDefaults.standard  userDefaults.set(arrayOfImage, forKey:"tableViewDataImage") userDefaults.set(arrayOfText, forKey:"tableViewDataText") userDefaults.synchronize()  var arrayOfImages = userDefaults.object(forKey: "tableViewDataImage") var arrayOfText = userDefaults.object(forKey: "tableViewDataText") 
like image 109
Tim Avatar answered Sep 20 '22 19:09

Tim


You can save your mutable array like this:

[[NSUserDefaults standardUserDefaults] setObject:yourArray forKey:@"YourKey"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

Later you get the mutable array back from user defaults. It is important that you get the mutable copy if you want to edit the array later.

NSMutableArray *yourArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"YourKey"] mutableCopy]; 

Then you simply set the UITableview data from your mutable array via the UITableView delegate

Hope this helps!

like image 36
Philipp Otto Avatar answered Sep 20 '22 19:09

Philipp Otto