How to Save Nsmutablearray in Nsuserdefaults

How to save NSMutablearray in NSUserDefaults

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")

Saving NSArray to NSUserDefaults and getting it in NSMutableArray

 [[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];

Saving NSMutableArray to NSUserDefaults

You must add

[currentDefaults synchronize];

each time after you save something to userDefaults;

But for such kind of data is better to use .plist files.
Check documentation for more info.

EDITED Code:

-(void)updateStrains:(NSDictionary *)item {
NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:item[@"strainsfinal"]];
[[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"strains"];
// NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
// [nc postNotificationName:@"arrayupdated" object:self userInfo:nil];
NSLog(@"updated strains %@",strainsfinal);
[[NSUserDefaults standardUserDefaults] synchronize]; // this will save you UserDefaults
}

Save NSMutableArray in NSUserDefaults

Try using these methods to save an array, a lot easier.

-(void)saveData :(NSMutableArray *)dataArray
{
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;

filemgr = [NSFileManager defaultManager];

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the data file
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"data.archive"]];

[NSKeyedArchiver archiveRootObject:
dataArray toFile:dataFilePath];
}

-(NSMutableArray *)loadData
{
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;

filemgr = [NSFileManager defaultManager];

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the data file
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"data.archive"]];

// Check if the file already exists
if ([filemgr fileExistsAtPath: dataFilePath])
{
NSMutableArray *dataArray;

dataArray = [NSKeyedUnarchiver
unarchiveObjectWithFile: dataFilePath];

return dataArray;
}
return NULL;
}


Related Topics



Leave a reply



Submit