how to get individual objects from NSUserdefaults Array - iphone

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.

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.

Does NSUserDefaults delete keys, that are not needed temporarily?

I'm storing all my data in NSUserDefaults.
Now I'm trying to store some keys with a specific prefix in an Array. Therefore, I first load the UserDefaults in a Dictionary.
NSString *myPrefix = #"prefix";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [defaults dictionaryRepresentation];
for (NSString *keyWithPrefix in dict.keyEnumerator) {
if ([keyWithPrefix hasPrefix: myPrefix]) {
[relevantKeys addObject: keyWithPrefix];
}
}
The Problem is: when I print "dict" (which represents UserDefaults). There are some keys missing.
Does NSUserDefaults delete keys, that are not needed temporarily?
Nope it does not, NSUserDefault is a persistance storage,
Please read the following answer it has a very good explanation
How persistent is [NSUserDefaults standardUserDefaults]?
No, but are you calling [NSUserdefaults synchronize] when you are updating them ?
You should probably be using -synchronize method when storing what you want. i,e:
[[NSUserDefaults standardUserDefaults] setValue:someDictionary forKey:#"someKey"];
[[NSUSerDefaults standardUserDefaults] synchronize];

XCode Cache Tutorial

Hey, I am trying to cache info on my IPhone App, but don't even know where to get started, I would to just cache a string (ex: 123123-12313123-1231231-123123), so when the user comes back it will remember it.
Is there a tutorial I can follow some where or could someone provide me some code?
Thanks
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setString:#"123123123" forKey:#"myKey"];
[userDefaults synchronize];
Later, when you wish to retrieve this object:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *myString = [userDefaults stringForKey:#"myKey"];
You'll, of course, want to manage the keys properly, etc.

strange issue with NSUserDefaults

I have an array in my app delegate.In some other class I am adding few dictionaries in this app delegate array.
There only I set this appdelegate array for a key in NSUserDefaults.Later on when I check NSUserDefaults value for key for which I saved the appdelegate array, always shows zero object.
I am using
[[NSUserDefaults standardUserDefaults] setObject:app.testArray forKey:#"theKey"];
You can try this code. I am very sure that it will work for you.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourArray forKey:#"init_val"];
[defaults synchronize];
For Retrieving Data You Should Use The Following Code :
NSMutableArray *newArray=[[[NSUserDefaults alloc] objectForKey:#"init_val"] mutableCopy];
Hope this helps you.
Try this for retrieving data
NSMutableArray *newArray=[[NSUserDefaults standardUserDefaults] objectForKey:#"init_val"];
Use object for key instead of valueforkey eg: [userdefaults objectForKey:#"init_val"];

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.