我想打印出我通过
类似于使用
NSUserDefaults
保存的所有值,而不需要提供特定的Key。类似于使用
for
循环在数组中打印所有值。有没有一种方法可以这样做?NSUserDefaults
保存的所有值,而不需要提供特定的Key。for
循环在数组中打印所有值。有没有一种方法可以这样做?Objective C
所有的值:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allValues]);
所有的键:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
所有的键和值:
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
使用 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())
您可以使用:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *defaultAsDic = [defaults dictionaryRepresentation];
NSArray *keyArr = [defaultAsDic allKeys];
for (NSString *key in keyArr)
{
NSLog(@"key [%@] => Value [%@]",key,[defaultAsDic valueForKey:key]);
}
仅打印键
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
键和值
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
您可以使用以下代码记录应用程序中所有可用的内容:
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);