I would like to do this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue#"nextFoo" forKeyPath#"values.foo"];
...
values was shoved into defaults earlier in the code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *values = [NSMutableDictionary alloc] init];
[values setObject#"firstFoo" forKey:#"foo"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:values forKey:#"values"];
I am guessing that regardless of the fact that I shoved a Mutable Dictionary into user defaults it gets stored as an immutable dictionary. I am trying to take a shortcut to allow me set a nested value without getting the values dictionary as mutable, setting the value there and then setting values again, i.e. longer version that I am trying to get around
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *values = [[defaults objectForKey:#"values"] mutableCopy];
[values setObject#"nextFoo" forKey:#"foo"];
[defaults setObject:values forKey:#"values"]; // set the entire values dictionary again, even though I really only needed to set one nested value
It's a common mistake, but you need to use the setObject: methods for NSUserDefaults, not setValue: and so the setValue:forKeyPath: also won't be of help to you. You'll need to just fetch the dictionary out and set the value then re-set the dictionary.
Related
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"
)
In my project I am using NSUseDefaults for store data with the different objects.
NSUserDefaults *defaults1=[NSUserDefaults standardUserDefaults];
//---- I have set object for this
[defaults1 synchronize];
NSUserDefaults *defaults2=[NSUserDefaults standardUserDefaults];
//---- I have set object for this
[defaults2 synchronize];
Now I want clear all keys data only for defaults2, not for defaults1. So whenever I am applying below code:
NSDictionary *defaultsDictionary = [defaults2 persistentDomainForName: appDomain];
for (NSString *key in [defaultsDictionary allKeys]) {
NSLog(#"removing user pref for %#", key);
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}
Above code have remove value for defaults2 but also for defaults1. But I don't want to remove objects for defaults1. So please help me out.
NSUserDefaults is like a singelton class so it will always return the same shared system object.
You can store multiple objects using multiple keys and can delete/remove objects against those keys.
If you have read a doc about NSUserDefaults standardUserDefaults you should know that standardUserDefaults Returns the shared defaults object. and actually defaults1 and defaults2 the same.
You can store keys and then delete only those keys like:
NSUserDefaults *defaults1=[NSUserDefaults standardUserDefaults];
//---- I have set object for this
[defaults1 synchronize];
[[defaults1 dictionaryRepresentation] allKeys]; // use this keys for deleting
I want to save data to a specific index in my table.
Here's what I want to achieve theoretically:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:cell.label.text forKey:indexPath];
What is the correct way of doing this?
Try this
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:cell.label.text forKey:[indexPath description]];
You can save the all the data that is relevant to the tableview in an array and store this array in standard user defaults.
EDIT
You CAN use NSUserDefaults for storing other objects than it originially supports.
Follow this question here:
How to save custom objects in array and store it in NSUserDefaults - iPhone
You can modify code as below
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:cell.label.text forKey:[NSString stringWithFormat:#"$$%d",indexPath.row]]; // modify as per your requirement
[userdefaults synchronize];
For Retrieving you can as below.
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
cell.textLabel.text=[userdefaults valueForKey:[NSString stringWithFormat:#"$$%d",indexPath.row]]; //modify as per your requirement
Hope it helps you...!
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.
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"];