Easy way to see saved NSUserDefaults?
You can print all current NSUserDefaults to the log:
Just keys:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
Keys and values:
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
You can find the pList file for your app in the simulator if you go to:
/users/your user name/Library/Application Support/iPhone Simulator/<Sim Version>/Applications
This directory has a bunch of GUID named directories. If you are working on a few apps there will be a few of them. So you need to find your app binary:
find . -name foo.app
./1BAB4C83-8E7E-4671-AC36-6043F8A9BFA7/foo.app
Then go to the Library/Preferences directory in the GUID directory. So:
cd 1BAB4C83-8E7E-4671-AC35-6043F8A9BFA7/Library/Preferences
You should find a file that looks like:
<Bundle Identifier>.foo.pList
Open this up in the pList editor and browse persisted values to your heart's content.
In Swift we can use the following:-
Swift 3.x & 4.x
For getting all keys & values:
for (key, value) in UserDefaults.standard.dictionaryRepresentation() {
print("\(key) = \(value) \n")
}
For retrieving the complete dictionary representation of user defaults:
print(Array(UserDefaults.standard.dictionaryRepresentation()))
For retrieving the keys:
// Using dump since the keys are an array of strings.
dump(Array(UserDefaults.standard.dictionaryRepresentation().keys))
For retrieving the values:
We can use dump here as well, but that will return the complete inheritance hierarchy of each element in the values array. If more information about the objects is required, then use dump, else go ahead with the normal print statement.
// dump(Array(UserDefaults.standard.dictionaryRepresentation().values))
print(Array(UserDefaults.standard.dictionaryRepresentation().values))
Swift 2.x
For retrieving the complete dictionary representation of user defaults:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation())
For retrieving the keys:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array)
For retrieving the values:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().values.array)