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...!
Related
When I save data using NSUserDefault first time it saves data successfully.When I close my application and start again then I get old data but again I use NSUserDefault with same key.The results is new data comes but my old data does not come.I want both old and new data. So what is the solution for this?
store data
NSUserDefaults *userDefaults ;
if(![[NSUserDefaults standardUserDefaults] objectForKey:#"mydata"])
{
userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:app.arr_defult forKey:#"mydata"];
[userDefaults synchronize];
}
else
{
NSLog(#"Leaderboards Dict Exists %#", [NSUserDefaults standardUserDefaults]);
userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:app.arr_defult forKey:#"mydata"];
[userDefaults synchronize];
}
read DAta :
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
arrayOfImages = [userDefaults objectForKey:#"mydata"];
It's not a good practice to store huge data set in NSUserDefaults.
NSUserDefaults is only for storing small amount of data like app preferences,
setting preferences, etc.
Use Core Data to handle your scenarios.
Everything is fine .just you add new data with same key first you get old data from nsuserdefault and add you both data(old and new) in nsuserdefault.i think is work fine..
// First Time
NSUserDefaults *userDefaults ;
userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:app.arr_defult forKey:#"mydata"];
[userDefaults synchronize];
// second Time
NSMutableArray *temparray1=[[NSMutableArray alloc] init];
[temparray1 addObject:[[NSUserDefaults standardUserDefaults] objectForKey:#"mydata"]];
[temparray1 addObject:app.arr_defult];
[userDefaults setObject:temparray1t forKey:#"mydata"];
[userDefaults synchronize];
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.
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"];
I have a list with a few rows, each row containing a switch. I would like to store the boolean values of the list (every time one is clicked) to nsuserdefaults , however im unsure of how to obtain each value. The switch is a UICustomSwitch. Thanks in advance!
Store booleans with
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:NO forKey:#"myKey"];
Retrieve booleans with
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs boolForKey:#"myKey"];
To integrate this with a swtich, you can do the following;
Store booleans with
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:mySwitch.on forKey:#"myKey"];
Retrieve booleans with
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[mySwitch setOn:[prefs boolForKey:#"myKey"] animated:YES];