Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get all values in NSUserDefaults? [duplicate]

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?

like image 624
Alik Rokar Avatar asked Jul 08 '13 08:07

Alik Rokar


People also ask

Where are the NSUserDefaults values stored?

All the contents saved by NSUserDefaults is saved inside a plist file that can be found under Library -> Preferences -> $AppBundleId.

Where can I find UserDefaults?

Navigate to AppData -> Library -> Preferences. The property list file (. plist) in this folder is the database file of the standard UserDefaults of your app.

What types can you store natively in NSUserDefaults?

Types stored in NSUserDefaultsplist type can be stored by NSUserDefaults . These types are NSString(String) , NSArray(Array) , NSDictionary(Dictionary) (for both NSArray and NSDictionary types their contents must be property list objects), NSNumber(Int, Float, Double, Boolean) , NSDate and NSData .

How do I get NSUserDefaults in Objective C?

you can access NSUserDefaults in any controller (Any class of your application) of your application with the same code you have written in one class. Show activity on this post. NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; NSString *myString = [defaults stringForKey:@"strings"];


2 Answers

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]); } 
like image 28
Midhun MP Avatar answered Sep 20 '22 19:09

Midhun MP


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()) 
like image 175
Anton Avatar answered Sep 21 '22 19:09

Anton