85

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?

Anupdas
  • 10,211
  • 2
  • 35
  • 60
Alik Rokar
  • 1,135
  • 1
  • 10
  • 16

4 Answers4

230

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())
Marián Černý
  • 15,096
  • 4
  • 70
  • 83
Anton
  • 2,797
  • 1
  • 12
  • 6
  • 1
    Swift 3 all values: print(UserDefaults.standard.dictionaryRepresentation().values) all keys: print(UserDefaults.standard.dictionaryRepresentation().keys) – davidrynn Jan 20 '17 at 22:41
6

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]);
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
5

Print only keys

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

Keys and Values

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
yunas
  • 4,143
  • 1
  • 32
  • 38
4

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

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Wain
  • 118,658
  • 15
  • 128
  • 151