NSUserDefaults returns before it has been set - iphone

Im making an app that takes two strings and an int. I want these to be displayed on another view.
I have put them into an array and Im trying to store them in the NSUserDefaults. I have managed to get this data stored. The problem that im having is that when I change the data and save it again The function that gets the array is called before the one that sets it . So the application always displays the previous data.
I thought It would be easy to fix by changing when the functions are called.
but from the code it looks to me lie they are in the right order.
2012-09-26 13:55:02.764 BeerDivider[4377:907] array returnd = (
2,
"Person 1",
"Person 2"
)
2012-09-26 13:55:02.773 BeerDivider[4377:907] array saved = (
5,
"Person 1",
"Person 2"
)
I can see from logging the array that they are called in the wrong order.
Please can anyone see where im going wrong. This is my first objective-c/xcode post so not sure what code you want to see. I will put in all of it.
http://pastebin.com/BX5gqp17
Sorry if this is a lot of code.
Thanks for the help.
EDIT: update the code
EDIT2: I think what is happening is similar to this iOS do the button's action before prepareSegue

First Take a look on Apple documentation of NSUserDefault
NSUserDefault always return the data which you saved in it
Dont forgot to write [defaults synchronize];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourArray forKey:#"YourKey"]
[defaults synchronize];
According to your given code.
-(void)saveInfo:(NSArray *)myArray
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myArray forKey:#"array"];
[defaults synchronize];
NSLog (#"array saved = %#", [defaults objectForKey:#"array"]);
}
and now check result

Do this:
-(void)saveInfo:(NSArray *)myArray
{
[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:#"array"];
[[NSUserDefaults standardUserDefaults] synchronize]; //u forgot this
NSLog (#"array saved = %#", myArray);
}

Related

Retrieval of NSArray from NSUserDefaults returns empty array

I am experiencing strange behaviour with NSUserDefaults. I am initially storing an array to the user defaults in my AppDelegate.m:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *weekdayIDs = [defaults objectForKey:#"weekdayIDs"];
if (weekdayIDs == nil) {
weekdayIDs = [NSArray arrayWithObjects:#"su", #"mo", #"tu", #"we", #"th", #"fr", #"sa", nil];
[defaults setObject:weekdayIDs forKey:#"weekdayIDs"];
}
[defaults synchronize];
Now in a different view controller ContentViewController.m, I want to retrieve the array:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *weekdayIDs = [defaults objectForKey:#"weekdayIDs"];
But I just get an array without objects, although its count == 7. I also used arrayForKey: but with the same result. I added a screenshot from my breakpoint.
I am regularly using NSUserDefaults, but currently I am bit stuck on this. It's probably a stupid mistake, anyone care to help?
Thank you so much!
-- Update:
I also figured it might be a problem with the init of the NSArray in the first place, but even replacing its objects with manually created NSString *dwid_su = [NSString stringWithString:#"su"]; didn't work.
Your code works perfectly.
Just, print the description of you array and you will see what you want.
Right click on weekdayIDs variable and select Print Description of weekdayIDs
or use through lldb debugger console po weekdayIDs
or NSLog(#"%#", weekdayIDs);
Here the results.

NSUserDefaults standardUserDefaults setObject: forKey: not working for Multivalue preference

I am trying to do following task
[[NSUserDefaults standardUserDefaults] setObject:#"Dry" forKey:#"vesselType_preference"];
[[NSUserDefaults standardUserDefaults] synchronize];
where my "vesselType_preference" is multivalue attribute, but it is not getting effected. Please help this is working for other type of attribute but not working for multivalue type.
Thanks
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"vesselType_preference"])
{
[defaults setObject:#"Dry" forKey:#"vesselType_preference"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
This should work.
NSUserDefaults can only handle objects of NSDictionary, NSData, NSArray, NSString and BOOL. (There might be another in there, not sure) If you need to store a multiple value object like an array or dictionary, I would store your settings there first, then save that to the defaults.
Your code looks fine for storing information to the user defaults. Just make sure you have your object type specified before saving. (id) will not work... or won't work properly.

Saving and Retrieving UILabel value with NSUserDefaults

I trying to save UILabel value to NSUserDefaults. I did IBAction with this code:
-(IBAction)saveData:(id)sender {
NSString *resultString = label.text;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:resultString forKey:#"result"];
[prefs synchronize];
}
Then, I connect it to button with Touch Up Inside.
What log shows after I pressed the button:
result = 0;
When I pressed a second time, then it works.
result = "28.34";
What I'm doing wrong and how can I retrieve a result?
EDIT
With this code I display result in log. I put it to same action.
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Your code looks correct, so the issue is probably not the function you posted but rather the code that contains your NSLog. You might be logging the value in a way that misses the first time it gets set.
Your log statement should be
NSLog(#"%#",[[NSUserDefaults
standardUserDefaults]
objectForKey:#"result"]);
This should be after you set the object in NSUserDefaults.
Add this to your applicationDidFinishLaunching in appDelegate or in init method in viewController(you have to create one) :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if(prefs == nil)
{
[prefs setObject:#"anything" forKey:#"result"];
[prefs synchronize];
}

Cannot set NSUserDefaults field

I have the following code in my initialization (first time) of my app:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
Much later in the code (in response to a button press) I check the value using:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *initVal=[defaults objectForKey:#"init_val"];
initVal is always nil. I have checked and init_val is set up exactly the same in my settings bundle as another field that I can set and read from without issue (they are both set up with a single field named "Key".
#mlewis54:
You can try this code. I am very sure that it will work for you.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
For Retrieving Data You Should Use The Following Code :
NSString *initVal=[[NSUserDefaults standardUserDefaults] valueForKey:#"init_val"];
OR
NSString *initVal=[[NSUserDefaults standardUserDefaults] objectForKey:#"init_val"];
EDIT:
If still it gives nil, then you can use the following code:
NSString *initVal=[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] valueForKey:#"init_val"]];
OR
NSString *initVal=[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"init_val"]];
Hope this helps you.
Decelare this at an event on click of button ...
NSUserDefaults *savedData = [NSUserDefaults standardUserDefaults];
[savedData setObject:userEmailTextField.text forKey:#"userid"];
and on load load the data and check the value you have entered is showing or not...
userEmailTextField.text =[[NSUserDefaults standardUserDefaults]objectForKey:#"userid"];
You need to use the line
NSString *initVal = [defaults stringForKey:#"init_val"];
- since it's a String you must use stringForKey. You can similarly use boolForKey to get a boolean value.
There's nothing wrong in your code, i tried to copy it in my project and it worked good,
so your error could be probably when you check initVal...
what's the code you use to see if initVal is nil?
and are you sure that when/where you check initVal, that var/oblect is still visible ?
try to insert this immediately after your reading code:
NSLog(#"____text read in pref: %#", initVal);
it writes this in my console window -> __text read in pref: 1
of course it's an NSString, i hope you keep it in mind it's not an int or a NSNumber when you check it...
luca
It might be that the initialisation isn't happening. Put an NSLog in there to check it's executed.
It works fine for me with the same code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
NSUserDefaults *defaultss = [NSUserDefaults standardUserDefaults];
NSString *initVal=[defaultss objectForKey:#"init_val"];
NSLog(#"Value of initVal: %#",initVal);
O/P on console: Value of initVal: 1
So try to do it with the same code with print on console.
Are you sure you properly initialize NSUserDefaults? When I initialize, in my app delegate, I always create the default user defaults by creating a dictionary of the default values then:
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Where appDefaults is the dictionary. Then you should be able to access them correctly.

how to get individual objects from NSUserdefaults Array

I am getting user details from .net web server.
I place all these in an array.
Now i need to place these array values in NSuserdefaults.
i try like this.
to store
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
[prefs setObject:resultData forKey:#"agentinfo"];
[prefs synchronize];
to get
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
[prefs arrayForKey:#"agentinfo"]
i am getting details like this
970,
Aditya2,
B,
JNTU1,
"Ram#gamil.com"
how can i get individual numbers 970,Aditya2...
Because i need to Assign them to label.
can any one pls help me.
Thank u in advance
stringForKey will always return nil if the object associated with the provided key is no string. Try arrayForKey instead.
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults)
yourArray = [standardUserDefaults objectForKey:#"agentinfo"];
use this code for access the array.