2013-07-08 91 views
38

我想打印所有通过NSUserDefaults保存的值,而无需提供特定的密钥。有没有办法在NSUserDefaults中获取所有值?

类似于使用for循环打印数组中的所有值。有没有办法做到这一点?

+1

在您的应用程序的域或系统域中? – awiebe

+7

http://stackoverflow.com/questions/1676938/easy-way-to-see-saved-nsuserdefaults – stosha

+1

对于Swift,你可以使用这个http://stackoverflow.com/a/27534573/1497737 – footyapps27

回答

135

目标C

所有值:

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

所有的键:

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

所有键和值:

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

使用:

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

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

夫特

所有值:

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

所有的键:

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

所有键和值:

print(UserDefaults.standard.dictionaryRepresentation()) 
+1

Swift 3所有值: print(UserDefaults.standard.dictionaryRepresentation()。values)所有键:print(UserDefaults.standard.dictionaryRepresentation()。键) – davidrynn

3

只打印键

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

键和值

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

可以使用记录所有提供给您的应用程序的内容:

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

您可以使用:

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

很容易理解 –

相关问题