我想打印所有值,我保存通过 NSUserDefaults没有提供一个特定的关键。
NSUserDefaults
类似于使用 for循环打印数组中的所有值。有办法这样做吗?
for
Print only keys
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
Keys and Values
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
You can log all of the contents available to your app using:
Objective C
all values:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allValues]);
all keys:
all keys and values:
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
print(UserDefaults.standard.dictionaryRepresentation().values)
print(UserDefaults.standard.dictionaryRepresentation().keys)
print(UserDefaults.standard.dictionaryRepresentation())
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]); }