How to add objects into NSMutableArray without deleting the existing ones? - ios

Im having trouble with my nsmutablearray in NSUserdefaults, this array every time I relaunch the app erases the objects that already are there and put the new ones, so I need help to prevent these to happen, Thanks and this is my code;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (!self.tasks) self.tasks = [NSMutableArray new];
[self.tasks addObject:textField.text];
[userDefaults setObject:self.tasks forKey:#"tasks"];
//[userDefaults synchronize];
NSLog(#"tasks:%#", [[NSUserDefaults standardUserDefaults]objectForKey:#"tasks"]);
NSLog(#"number of tasks:%d", self.tasks.count);
And Im reading it in a tableview this way:
cell.taskTitle.text = (self.TasksArray)[indexPath.row];
Thanks!

You are missing a line of code there:
self.tasks = [[NSMutableArray alloc] initWithArray:[userDefaults objectForKey:#"tasks"];
As far as I can tell, you're not initially setting your ".tasks" property, so adding that bit may fix the problem.

You need to get it from user defaults first as rmaddy suggests. Your nil check will then create a new array if its never been created/saved before.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
self.tasks = [[userDefaults objectForKey:#"tasks"] mutableCopy];
if (!self.tasks) self.tasks = [NSMutableArray new];
[self.tasks addObject:textField.text];
[userDefaults setObject:self.tasks forKey:#"tasks"];
//[userDefaults synchronize];
NSLog(#"tasks:%#", [[NSUserDefaults standardUserDefaults]objectForKey:#"tasks"]);
NSLog(#"number of tasks:%d", self.tasks.count);

I see you never call [[NSUserDefaults standardUserDefaults] synchronize];
Once you finish with changes of your object you should call it since it saves your changes to disk.

Related

Objective-C NSUserDefaults don't save NSMutableArray for specific key

I have strange problem with NSUserDefaults.
I would like to save NSMutableArray.
I have this piece of code:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:selected forKey:#"markListArr"];
The problem is that the object for key "markListArr" doesn't get saved.
I do this at the first run of the app.
When later in the app I want to save object for that key everything works fine.
If I use any other key everything works fine. I would like to use that specific key because I already have app on app store and this is only update to the existing app.
I already tried [userDefaults synchronize] and it doesn't work.
Any ideas what is happening?
Are you calling [userDefaults synchronize] before fetching the object? I'm using the code below:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *selected = [[NSMutableArray alloc] init];
[selected addObject:#"New Value"];
[userDefaults setObject:selected forKey:#"markListArr"];
[userDefaults synchronize];
NSMutableArray *arr = [userDefaults objectForKey:#"markListArr"];
NSLog(#"arr: %#", arr);
and it prints:
arr: (
"New Value"
)

how to call nsuserdefaults to tableview

i have two textfields and an array.
NSMutableArray *myArray = [[NSMutableArray alloc]init];
[myArray addObject:textFieldHeader.text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myArray forKey:textFieldGroup.text];
textFieldGroup.text passing to tableview and
-(void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
array = [[NSMutableArray alloc] initWithArray:[standardUserDefaults objectForKey:_passedFieldGroup]];
[self.tableView reloadData];
}
but how do i call these keys as sections and objects as rows when i go to table view?
As far as I understand your problem, you want to go through all of the stored keys in the NSUserDefaults?
That is not possible as far as I know. The only solution that comes to my mind is to store the keys seperate in a file or maybe in an array in the NSUserDefaults, too.

How to read an Array stored in NSUserDefaults?

Im storing a NsMutableArray in Nsuserdefaults, I think this can be done and its correct, so Im adding a text from a TexField to the Array but when I try to read it in a NSLOG sends null, heres the code:
#property(nonatomic, strong)NSMutableArray *tasks;
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:self.tasks forKey:#"tasks"];
[self.tasks addObject:textField.text];
NSLog(#"tasks:%#", [[NSUserDefaults standardUserDefaults]objectForKey:#"tasks"]);
Thanks!!
You need to add the text to the array BEFORE you save it to defaults
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
// make sure self.tasks is initialized
if (!self.tasks) self.tasks = [NSMutableArray new];
// add object first
[self.tasks addObject:textField.text];
// now save array
[standardUserDefaults setObject:self.tasks forKey:#"tasks"];
NSLog(#"tasks:%#", [[NSUserDefaults standardUserDefaults]objectForKey:#"tasks"]);
If this is returning null, then

iOS: Storing selected table view rows in userDefaults

In my app I have a tableview which allows user to enable or disable elements by tapping the rows. Quite simply, I need to store each row's condition in userDefaults - if it's turned on, or not. How would I go about this?
I was thinking I could add a BOOL property to the object each row represents for whether or not its enabled but how would I go about doing remembering the value of the property for each individual object?
You could use some kind of id on each row to be able to identify a particular row. Then you could create an custom object and store it in NSUserDefaults.
Look at this so question
You can just create a bunch of setting keys:
// SET
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"setting1_ON"];
[defaults setBool:NO forKey:#"setting2_ON"];
// GET
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool setting1 = [defaults boolForKey:#"setting1_ON"];
bool setting2 = [defaults boolForKey:#"setting2_ON"];
Or store one array of all settings (or dictionary):
// SET
NSMutableArray *settings = [NSMutableArray array];
for (UITableViewCell *c in [self.tableView subviews]) {
if (c.selected) {
[settings addObject:[NSNumber numberWithBool:YES]];
} else {
[settings addObject:[NSNumber numberWithBool:NO]];
}
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:settings forKey:#"settings"];
// GET
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *mySettings = (NSMutableArray *)[defaults objectForKey:#"settings"];
setting1 = [mySettings objectAtIndex:1];
setting2 = [mySettings objectAtIndex:2];
On that note, you might consider 2 other options... 1. Just keep all your BOOLs in an array instead of adding them to the cells, then store it when needed; 2. Create a NSObject that is your settings, keep them all in there, and then store the entire object:
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:myApp.settings];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myEncodedObject forKey:#"settings"];
To do this you have to add encode/decode stuff to that settings object... there's more on storing custom objects around these parts.

NSMutableArray stored in NSUserDefaults won't save data

When a user signs in through my iPhone app, I create a bunch of default elements in the NSUserDefaults. One of these is an NSMutableArray that I add the following way:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableArray *theArray = [NSMutableArray array];
[prefs setObject:theArray forKey:#"theArray"];
This works flawlessly. However, when I want to insert or retrieve values from theArray, something goes wrong. Here's an excerpt from another file in my app:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[[prefs objectForKey:#"theArray"] setValue:#"This is a value" forKey:#"aValue"];
NSLog(#"%#", [[prefs objectForKey:#"theArray"] valueForKey:#"aValue"]);
I would expect to see "This is a value" in the console when the code has run, but instead I get this:
2011-08-08 18:35:17.503 MyApp[7993:10d03] (
)
How come the array is empty? I've tried the same thing using an NSArray with the same result.
When you store mutable objects to NSUserDefaults, it stores an immutable copy of it so you can't change it directly like that. You have to get the mutable copy out of defaults, change it, and then set it back, replacing old object in defaults.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableArray *mutableArrayCopy = [[prefs objectForKey:#"theArray"] mutableCopy];
[mutableArrayCopy addObject:#"some new value"];
[prefs setObject:mutableArrayCopy forKey:#"theArray"];
[mutableArrayCopy release];
NSArray *savedArray = [NSArray arrayWithObjects:#"obj1",#"obj2",#"obj3", nil];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:savedArray forKey:#"key_for_savedArray"];
[userDefaults synchronize];
//To retrive array use:
NSArray *retrivedArray = [userDefaults objectForKey:#"key_for_savedArray"];

Resources