2013-07-08 17 views
38

Chciałbym wydrukować wszystkie wartości zapisane przez NSUserDefaults bez podawania konkretnego klucza.Czy istnieje sposób na uzyskanie wszystkich wartości w NSUserDefaults?

Coś jak drukowanie wszystkich wartości w tablicy przy użyciu pętli for. Czy jest jakiś sposób na zrobienie tego?

+1

W domenie aplikacji lub domenie systemowej? – awiebe

+7

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

+1

Dla Swift możesz użyć tego http://stackoverflow.com/a/27534573/1497737 – footyapps27

Odpowiedz

135

Objective C

wszystkie wartości:

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

wszystkie klucze:

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

wszystkie klucze i wartości:

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

wykorzystujące do:

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

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

Swift

wszystkie wartości:

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

wszystkie klucze:

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

wszystkie klucze i wartości:

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

Swift 3 wszystkie wartości: print (UserDefaults.standard.dictionaryRepresentation(). values) all keys: print (UserDefaults.standard.dictionaryRepresentation(). keys) – davidrynn

3

drukować tylko klucze

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

klucze i wartości

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

Można rejestrować wszystkie treści dostępnych aplikacji przy użyciu:

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

Można użyć:

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

całkiem zrozumiałe –

Powiązane problemy