Is there a way to get all values in NSUserDefaults?

IphoneIosObjective CIos6Nsuserdefaults

Iphone Problem Overview


I would like to print all values I saved via NSUserDefaults without supplying a specific Key.

Something like printing all values in an array using for loop. Is there a way to do so?

Iphone Solutions


Solution 1 - Iphone

Objective C

all values:

NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allValues]);

all keys:

NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);

all keys and values:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

using for:

NSArray *keys = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys];

for(NSString* key in keys){
    // your code here
    NSLog(@"value: %@ forKey: %@",[[NSUserDefaults standardUserDefaults] valueForKey:key],key);
}

Swift

all values:

print(UserDefaults.standard.dictionaryRepresentation().values)

all keys:

print(UserDefaults.standard.dictionaryRepresentation().keys)

all keys and values:

print(UserDefaults.standard.dictionaryRepresentation())

Solution 2 - Iphone

You can use:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *defaultAsDic = [defaults dictionaryRepresentation];
NSArray *keyArr = [defaultAsDic allKeys];
for (NSString *key in keyArr)
{
     NSLog(@"key [%@] => Value [%@]",key,[defaultAsDic valueForKey:key]);
}

Solution 3 - Iphone

Print only keys

NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);

Keys and Values

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

Solution 4 - Iphone

You can log all of the contents available to your app using:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlik RokarView Question on Stackoverflow
Solution 1 - IphoneAntonView Answer on Stackoverflow
Solution 2 - IphoneMidhun MPView Answer on Stackoverflow
Solution 3 - IphoneyunasView Answer on Stackoverflow
Solution 4 - IphoneWainView Answer on Stackoverflow