Can we update the array stored in NSUserDefault - iphone

I am having values stored in an array, and stored that array in NSUserDefault, then i need to update that array can i do so. If then how?

First retrieve the current data.
NSArray *tempNew = [[NSArray alloc]init];
tempNew = [storeData objectForKey:#"accounts"];
[tempArr addObjectsFromArray:tempNew];
Update:
[tempArr addObject:str];
[storeData setObject:tempArr forKey:#"accounts"];

NSUserDefaults is just as an NSDictionary, so you can use the same method of updating a dictionary by setting another object to the key value -setObject:forKey:.
for example when you try below code
NSUserDefaults *usr=[NSUserDefaults standardUserDefaults];
NSArray *arr= [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], nil];
[usr setObject:arr forKey:#"num"];
NSLog(#"usr %#",[usr arrayForKey:#"num"]);
NSUserDefaults *usr1=[NSUserDefaults standardUserDefaults];
NSArray *arr= [NSArray arrayWithObjects:[NSNumber numberWithInt:2],[NSNumber
numberWithInt:3], nil];
[usr setObject:arr forKey:#"num"];
NSLog(#"user1 %#",[usr1 arrayForKey:#"num"]);
u will get
usr (
1,
2
)
usr1 (
2,
3
)
in console.

Just re-store the array in the user defaults and call synchronize.
So for example:
Store your array...
NSMutableArray *yourArray = .... // whatever you did to create your data
// store your data in the NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourArray forKey:#"myArray"];
[defaults synchronize];
... and then some other time somewhere in your app update it!
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// retrieve your data from the NSUserDefaults
NSMutableArray *yourArray = [defaults objectForKey:#"myArray"];
// edit the data, for example add some object.
[yourArray addObject:someNewObject];
// update the defaults
[defaults setObject:yourArray forKey:#"myArray"];
[defaults synchronize];

Related

How to set values in NSUserDefaults?

I have this:
NSMutableDictionary *responseDict = [responseString objectFromJSONString];
NSLog(#"%#" , [responseDict objectForKey:#"loginid"]);
and i want to put my "loginid" to NSUserDefault..
how to put the loginid value to NSUserDefault, and Retrieve ?
i tried this way but not working properly
[defaults setInteger:[responseDict objectForKey:#"loginid"] forKey:#"user_id"];
Please need help
thanks in advance
To put in NSUserDefaults you can do this
For saving:
NSString *logInId = [NSString stringWithFormat:#"%#",[responseDict objectForKey:#"loginid"]];
NSUserDefaults *preferences = [NSUserDefaults standardDefaults];
[preferences setValue:logInId forKey:#"logInId"];
[preferences synchronize];
For retrieving:
NSUserDefaults *preferences = [NSUserDefaults standardDefaults];
NSString *logInIdValue = [preferences valueForKey:#"logInId"];

How to remove user defaults [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I reset the NSUserDefaults data in the iPhone simulator?
In my project i am saving some values in NSUserdefaults with different keys.And i have reset button in my app when i click that button values stored in user defaults should remove.Is there any way to delete those values without keys,and is addSuitedNamed: and removeSuitedName can use in this scenario.
NSUserDefaults * myNSUserDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [myNSUserDefaults dictionaryRepresentation];
for (id key in dict) {
//heck the keys if u need
[myNSUserDefaults removeObjectForKey:key];
}
[myNSUserDefaults synchronize];
or
[NSUserDefaults resetStandardUserDefaults];
[NSUserDefaults standardUserDefaults];
With a selector being called on a button click you can achieve the same using
- (IBAction)btnResetUserDefaultsPressed:(id)sender {
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary* dictUserDefaults = [userDefaults dictionaryRepresentation];
for (id akey in dictUserDefaults) {
[userDefaults removeObjectForKey:akey];
}
[userDefaults synchronize];
}
For the second part of your question asked , You would certainly find this useful.
You can reset the values of NSUserDefault using resetStandardUserDefaults.
Check this code:
[NSUserDefaults resetStandardUserDefaults];
[NSUserDefaults standardUserDefaults];
Also you can:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
Or you can use:
NSUSerDefaults *default = [NSUserDefaults standardUserDefaults];
NSDictionary *dictionary = [default dictionaryRepresentation];
for (NSString *key in [dictionary allKeys])
{
[default removeObjectForKey:key];
}
[default synchronize];

Array within a NSDictionary: how to save to the NSUserDefaults

I would like to save Array/NSDictionary to NSUserDefaults but anything I try is just not working. Here is my code so please if you know how to do this, help me.
NSArray *oneArray = [NSArray arrayWithObjects:#"Radio One",nil];
NSDictionary *one = [NSDictionary dictionaryWithObject:oneArray forKey:#"Stations"];
NSArray *oneLinkArray = [NSArray arrayWithObjects:#"http://mobile.com:28000/",nil];
NSDictionary *oneLink = [NSDictionary dictionaryWithObject:oneLinkArray forKey:#"Stations"];
[data addObject:one];
[link addObject:oneLink];
The reason I need this is to put this station into favorites. So my thinking is to save these info in to NSUserDefaults and retrieve in favorites table.
Thanks and please any suggestion is welcomed and appreciated.
You can use something like this when you store everything into a dictionary
[[NSUserDefaults standardUserDefaults] setObject:link forKey:#"dictionary1"];
Or you can put everything into an array and store it like this
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"array1"];
You can access it again by using
NSDictionary * myDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:#"dictionary1"];
Typically adding an object into NSUserDefaults goes like this:
NSMutableDictionary *dictionaryToAdd = [[NSMutableDictionary alloc] init];
[dictionaryToAdd setObject:#"xyz" forKey:#"myKey"];
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:dictionaryToAdd forKey:#"someKey"];
[myDefaults synchronize];
A few things on your code above though - you're adding stuff into 'data' and 'link' but I don't see those in your code, so I'm assuming those arrays exist somewhere.
To sum it up - declare an NSUserDefaults object, set objects into it like an NSDictionary, and then synchronize it to save data.
Additional code as requested:
//You have an array named arrayToAdd that has already been created
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:arrayToAdd forKey:#"SomeKeyThatYouMakeUp"];
[myDefaults synchronize];
//You want to get the array out of NSUserDefaults
NSArray *mySavedArray;
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
mySavedArray = [myDefaults objectForKey:#"SomeKeyThatYouMakeUp"];

retrieve values from NSUserDefault in iPhone after setting again

In App delegate, I have following code:
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
NSString *alrmTime = #"10:00 AM";
[pref setObject:alrmTime forKey:#"alarmTime"];
[prefs synchronize];
From here I am getting from App delegate User Daeault in Controller A using code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *tempAlarmTime = [defaults stringForKey:#"alarmTime"];
cell.textLabel.text = [NSString stringWithFormat:#"Remind At %#", tempAlarmTime];
Now, I need to set this userdefault in Controler B , For this m using this:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:mTimeIntervalSTR forKey:#"alarmTime"];
[prefs synchronize];
Now when I need to get this new value in Controller A its coming Null. Why this is happening, and how will I get new set value?
in Controler B use the following to change the userdefault value
NSString *timeString =[[[NSUserDefaults standardUserDefaults] objectForKey:#"alarmTime
"]mutableCopy];
timeString = mTimeIntervalSTR;
[[NSUserDefaults standardUserDefaults]setObject:timeString
forKey:#"alarmTime "];
[[NSUserDefaults standardUserDefaults]synchronize];
After adding a value, call [[NSUserDefaults standardUserDefaults] synchronize];.

How to keep my data in a array-iphone sdk

I'm trying to create an application, and in that I'm receiving some contents from net and loading into and array, if I quit and run the app again i'm losing the data. How can I store the data in the app itself. Should I use some kind of database? If so which one and how can I implement that? I need to store only some string variables.
EDIT:
My app is tabbar based app, and have two tabs. Loading the arrays in one tab and loading that array in a table in the other VC. once I load the array and move to the second tab, the table is loaded with the array. If i go back and add few more values and then if i check, the added datas is not displayed in the table. And also, when I quit the app the table lose all the values, I need to keep the values in the table, even after i quit the app. How can I do that?
here is my code:
in viewdidload:
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:alertList forKey:#"myArray"];
[simplePlistDb synchronize];
in cellforrowatindexpath:-
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray){
NSLog(#"Str:%#", str);
[loadArray addObject:str];
}
cell.textLabel.text = [loadArray objectAtIndex:indexPath.row];
}
Thank you.
You will want to look into Core Data if you want to save a decent amount of data to the iPhone. However, if not, you can just load the items in the array into a plist and load them from there at launch.
Plist Writing: How to create a new custom property list in iPhone Applications
CoreData: http://www.raywenderlich.com/934/core-data-tutorial-getting-started
Edit--->
As Nekto said, you can also use NSUserDefaults, but I advise mainly using NSUserDefaults for simple NSStrings and integers.
I think for your purposes NSUserDefaults will be enough : NSUserDefaults Class Reference.
Examples:
// save
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:[NSArray arrayWithObjects:#"very", #"cool", nil]];
[simplePlistDb synchronize];
// restore
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray)
NSLog(#"%#", str);
}
You can use NSUserDefaults in order to store your data till the time your app is installed in your device.
How to write in NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"yourObject1" forKey:#"correspopndingKey1"];
[defaults setObject:#"yourObject2" forKey:#"correspopndingKey2"];
[defaults synchronize];
How to read from NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *temp1 = [defaults objectForKey:#"correspopndingKey1"];
NSString *temp2 = [defaults objectForKey:#"correspopndingKey2"];
You can store NSArray in a similar way.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:objArray forKey:#"correspopndingKey"];
[defaults synchronize];