how to save old and new data with NSUserDefault - ios

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

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

Clear data of a particular NSUserDefaults object Data

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

How to set Default Object at indexPath?

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...!

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

Retrieve and store boolean values in uitableview ios

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

Resources