Can you see the values of NSUserDefaults naywhere in the xcode debugger?
Just wondering if this is possible?
Thanks,
Nick
I don't have a solution to view them in the debugger, but I can offer this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%#", [defaults dictionaryRepresentation]);
For some caveman-debugging:)
EDIT: As David suggest in the comment, we can now do this in the debugging console:
po [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]
Swift 3.0
po UserDefaults.standard.dictionaryRepresentation()
I haven't done it but you should be able to execute a po (print object) command on the user defaults like so:
po [[NSUserDefaults standardUserDefaults] valueForKey:#"someKeyName"]
I prefer to wrap my defaults in a custom class and create a description method that dumps the defaults.
You can use the "defaults" command line utility to examine the defaults exactly. Read the man page for details.
Not aware of any GUI that displays NSUserDefaults, but I use this in my application delegate to see the settings at start up:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%# DEFAULTS = %#", [self class], [defaults persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]);
}
You can log or either use PO command at debugger
for Keys :
NSLog(#"%#", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
or for Keys and values:
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
& on debugger use:
getting all keys :
po [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]
for key & values :
po [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]
Updated for Swift 5
Print all UserDefault key-value pairs to console:
print(UserDefaults.standard.dictionaryRepresentation())
Print UserDefault keys to console:
print(UserDefaults.standard.dictionaryRepresentation().keys)
Print UserDefault values to console:
print(UserDefaults.standard.dictionaryRepresentation().values)
read:
po UserDefaults.standard.value(forKey: "key")
write:
po UserDefaults.standard.set(true, forKey: "key")
you can also add an expression directly on the Variable breakpoint view and adding : UserDefaults.standard.dictionaryRepresentation() see the image below .
add breakpoint view
Related
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.
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.
Is this code correct to load and save NSUserDefaults.
// Load
NSDictionary *dict = [[NSUserDefaults standardUserDefaults]
dictionaryRepresentation];
// Save
NSDictionary *dict = ....
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:dict];
[defaults synchronize]; << Not sure if this is needed
I'm using JSON to hold the dictionary contents and I'm having problems.
I'd just like to know if this code is correct, before I look elsewhere for my problem.
Your Load code will get you a dictionary of all the current default values.
[Note This will probably be much larger than you expect as Mac OS installs a raft of defaults, you might want to make it smaller. For example, you can limit the dictionary to just those defaults in your domain which differ from their registered default using:
NSDictionary *userDefaults = [[NSUserDefaults standardUserDefaults] persistentDomainForName:#"<your bundle identifier>"];
end note]
Your Save code probably doesn't do what you expect, it installs the values you are restoring as the defaults for those keys - so if you support "Restore to Default Settings", or something similar, then these are the values that would result. What you probably want to do it set the current value of the keys, this can be done with a simple loop:
NSDictionary *dict = ....
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
foreach(id key in dict)
[defaults setObject:[dict objectForKey:key] forKey:key];
The above code is only an outline, you may need to take care what preferences you save/restore and in what order - but all that depends on your application.
// Get
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:#"myDictionary"];
// Set
NSDictionary *dict = ...
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:#"myDictionary"];
I want to access text saved through NSUserDefaults and display it in a label that has already been defined called "name". My code is below, but it doesn't work. What should I do? Thanks for your help!
name = [[NSUserDefaults standardUserDefaults] objectForKey:#"Name"];
should be:
name.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"Name"];
First, you should register your defaults someway like this: (this is only necessary if you want to add multiple items!)
NSDictionary *defaultsDict =
[NSDictionary dictionaryWithObjectsAndKeys:#"MyName", #"defaultName", #"MyAge", #"defaultAge", nil];`
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];
Now use this assuming you have a label pointer:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[label setText:[defaults stringForKey:#"defaultName"]];
Been working on some code streamlining and have realised that it would be really helpful if my app had a preferences system.
Now here's how my code works.
A method runs based upon an integer stored in NSUserDefaults
e.g.
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"scifi1"] == 040){
[self spaceDown];
}
else if ([[NSUserDefaults standardUserDefaults] integerForKey:#"scifi1"] == 10040){
[self ctrldown];
[self spaceDown];
}
Now what I want to do is when I exit the view (via a specific button) is to dump the value of #"scifi1" into a new preference, say for example - an integer named #"savedscifi1"
Now I know how to save integers into NSUserDefaults,
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:VALUEHERE forKey:#"savedscifi1"];
[userDefaults synchronize];
However - I'm not sure how I can substiture in the value of scifi1 instead of (in this case) 'VALUEHERE' - can anyone help with this? I feel it's really simple but I can't help but think I'm being a bit thick...sleep deprived and approaching a deadline! I know I can't just call up #"scifi1"but beyond that....??
NSInteger value = [[NSUserDefaults standardUserDefaults] integerForKey: ...];
[[NSUserDefaults standardUserDefaults] setInteger: value forKey: ...];