17

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!

6 Answers 6

36
NSLog(@"NSUserDefaults dump: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
4
  • @rpetrich: Thanks. I put it in and it did indeed dump the output to the debugger's console, but the information that was dumped look more like System specific user defaults, including printer preferences, time zone prefs, etc... Do you know of a way to only pull out app specific userDefaults on the iPhone? Kevin Apr 30, 2009 at 23:58
  • 1
    NSUserDefaults are stored at /User/Applications/{UNIQUE_ID}/Library/Preferences/com.yourcompany.yourapp.plist; you should be able to get the path up to {UNIQUE_ID} by retrieving [[NSBundle mainBundle] bundlePath] and then navigating up one level, but I don't recommend relying on that file to stay in the same place between OS versions.
    – rpetrich
    May 1, 2009 at 0:14
  • Also, if you need to have a number of keys that you don't know at compile time, you should store them in a NSDictionary and then store that in your NSUserDefaults so as not to collide with Apple's settings.
    – rpetrich
    May 1, 2009 at 0:17
  • Yeah, I was just thinking about that while looking at the dump. I'd really hate to overwrite an Apple key-value pair - if I even could. May 1, 2009 at 0:45
19

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!

1
  • Glorious. This needs to be higher up in the answer list. Jan 12, 2017 at 22:57
9

Try:

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

dictionaryRepresentation returns an NSDictionary representation of the defaults.

2
  • @thesamet: Thanks to you too. It's like the two of you were channeling the same iPhone dev spirit. (^_^) Please take a look at my response to rpetrich. Maybe you will have a answer - faster. (^_^) Thanks again! Apr 30, 2009 at 23:59
  • 1
    Check if you have the application bundle identifier set in info.plist
    – thesamet
    May 1, 2009 at 0:07
8
NSLog(@"%@ defaults = %@", [self class], 
  [[NSUserDefaults standardUserDefaults] 
   persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]);
6

The shared NSUserDefaults is initialized with three search domains by default (you can add others too if you need to): app arguments, app preferences (what's stored in the app's plist), and localized system preferences. The last one is why you're seeing those unfamiliar Apple keys, but you don't really have to worry about "overwriting" them. If you use the same key name, it will just put that value in the app preferences domain. Your app's preferences is searched before the system preferences so you'll get the same value back, but it won't affect anything else.

If you really do want just your app's preferences though, you can remove the other search domains (the specific names you need is in the docs).

2
  • Hey, thanks Marc. Any chance you could point me in the right direction of how to modify the code: NSLog(@"NSUserDefaults dump: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]); ...to search for just the app's userDefaults? May 1, 2009 at 5:35
  • 1
    You need to remove the other search domains (I think the method is something like removeSuiteNamed:?) before calling dictionaryRepresentation. I don't remember the domain names offhand, but you'll find them in the NSUserDefaults documentation. May 1, 2009 at 16:15
1

Here is a handy class that you can use to dump all kinds of crap:

https://github.com/VTPG/CommonCode/blob/master/VTPG_Common.h

To use it, add the header to your "m"

#import "VTPG_Common.h"

then anywhere in your class call LOG_EXPR(foo):

NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
[userDefaults addSuiteNamed:@"com.apple.spaces"];
NSDictionary *foo = [userDefaults dictionaryForKey:@"SpacesDisplayConfiguration"];
LOG_EXPR(foo);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.