Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults: Dumping the structure of NSUserDefaults's standardUserDefaults

Does anyone know of a quick way to dump the standardUserDefaults of NSUserDefaults via NSLog? This is what I have:

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

But it returns:

NSUserDefaults dump: <NSUserDefaults: 0x50b520>

...which is not quite what I'm looking for. I'd really like to have key-value pairs.

Any help or a point in the right direction would be greatly appreciated. Cheers!

like image 346
Kevin Bomberry Avatar asked Apr 30 '09 23:04

Kevin Bomberry


4 Answers

NSLog(@"NSUserDefaults dump: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
like image 78
rpetrich Avatar answered Nov 04 '22 03:11

rpetrich


Thanks to Don McCaughey, my business partner and friend, for fixing up my code for me and supply a concise answer. To share it with the rest of you here is a code snippet:

  NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];
  NSString *bundleId = [bundleInfo objectForKey: @"CFBundleIdentifier"];

  NSUserDefaults *appUserDefaults = [[NSUserDefaults alloc] init];
  NSLog(@"Start dumping userDefaults for %@", bundleId);
  NSLog(@"userDefaults dump: %@", [appUserDefaults persistentDomainForName: bundleId]);
  NSLog(@"Finished dumping userDefaults for %@", bundleId);
  [appUserDefaults release];

As you can see, everyone who was answering the question was on the right track, but no code offered up was the solution - until Don's editing of our code in source control. Thanks All!

like image 30
Kevin Bomberry Avatar answered Nov 04 '22 03:11

Kevin Bomberry


Try:

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

dictionaryRepresentation returns an NSDictionary representation of the defaults.

like image 9
thesamet Avatar answered Nov 04 '22 01:11

thesamet


NSLog(@"%@ defaults = %@", [self class], 
  [[NSUserDefaults standardUserDefaults] 
   persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]);
like image 8
Elise van Looij Avatar answered Nov 04 '22 02:11

Elise van Looij