views:

1678

answers:

5

Hi hi. 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!

+8  A: 
NSLog(@"NSUserDefaults dump: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
rpetrich
@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
Kevin Bomberry
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
@prtrich: Thanks, I'll go take a look at that. (^_^)
Kevin Bomberry
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
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.
Kevin Bomberry
+4  A: 

Try:

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

dictionaryRepresentation returns an NSDictionary representation of the defaults.

thesamet
@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!
Kevin Bomberry
Check if you have the application bundle identifier set in info.plist
thesamet
@thesamet: Thanks, I'll go take a look at that too. (^_^)
Kevin Bomberry
+5  A: 

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).

Marc Charbonneau
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?
Kevin Bomberry
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.
Marc Charbonneau
+4  A: 

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!

Kevin Bomberry
+3  A: 
NSLog(@"%@ defaults = %@", [self class], 
  [[NSUserDefaults standardUserDefaults] 
   persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]);
Elise van Looij