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?
All the contents saved by NSUserDefaults is saved inside a plist file that can be found under Library -> Preferences -> $AppBundleId.
Navigate to AppData -> Library -> Preferences. The property list file (. plist) in this folder is the database file of the standard UserDefaults of your app.
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 .
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"];
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]); }
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With