views:

1464

answers:

6

Is there a way to see what's been saved to NSUserDefaults directly? I'd like to see if my data saved correctly.

+1  A: 

You could NSLog each value you set, like:

NSLog(@"%@",[[NSUserDefaults standardDefaults] stringForKey:@"WhateverTheKeyYouSet"]);

Tom Irving
+1  A: 

you can check values for each key in array, returned by

[[[NSUserDefaults standartDefaults] dictionaryRepresentation] allKeys]
Morion
+7  A: 

You can find the pList file for your app in the simulator if you go to:

/users/your user name/Library/Application Support/iPhone Simulator/User/Applications

This directory has a bunch of GUID named directories. If you are working on a few apps there will be a few of them. So you need to find your app binary:

find . -name foo.app
./1BAB4C83-8E7E-4671-AC36-6043F8A9BFA7/foo.app

Then go to the Library/Preferences directory in the GUID directory. So:

cd 1BAB4C83-8E7E-4671-AC35-6043F8A9BFA7/Library/Preferences

You should find a file that looks like:

<Bundle Identifier>.foo.pList

Open this up in the pList editor and browse persisted values to your heart's content.

Cannonade
+1  A: 

I sometimes use the following snippet to print out the location of my NSUserDefaults file when running in the simulator

NSArray *path = NSSearchPathForDirectoriesInDomains(
   NSLibraryDirectory, NSUserDomainMask, YES);
NSString *folder = [path objectAtIndex:0];
NSLog(@"Your NSUserDefaults are stored in this folder: %@/Preferences", folder);

It yields the path to the preferences folder

Your NSUserDefaults are stored in this folder: /Users/castle/Library/Application Support/iPhone Simulator/User/Applications/BC5056A0-F46B-4AF1-A6DC-3A7DAB984960/Library/Preferences

Your NSUserDefaults file is located in the preferences folder and named according to your prefix and appliation name e.g.

dk.castleandersen.dreamteam.grid.plist

I expect the same to be true for the actual device.

Niels Castle
+1  A: 

I keep a shortcut on my desktop to the simulator's folder where it keeps the apps, ie:

/Users/gary/Library/Application Support/iPhone Simulator/User/Applications

Sorted by most recent date, then just go into the most recent app folder Library/Preferences and view the file in the plist editor.

progrmr
+3  A: 

Print all current NSUserDefaults to the log:

Just keys:

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

Keys and values:

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

marshn