Easy way to see saved NSUserDefaults?

2019-01-02 19:16发布

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

19条回答
后来的你喜欢了谁
2楼-- · 2019-01-02 19:46

I built this method based on Morion's suggestion for better presentation. Use it by calling [self logAllUserDefaults]

- (void) logAllUserDefaults
{
    NSArray *keys = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys];
    NSArray *values = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allValues];
    for (int i = 0; i < keys.count; i++) {
        NSLog(@"%@: %@", [keys objectAtIndex:i], [values objectAtIndex:i]);
    }
}
查看更多
忆尘夕之涩
3楼-- · 2019-01-02 19:47

In Swift 4.0

//func dictionaryRepresentation() -> [String : AnyObject]

because dictionaryRepresentation of NSUserDefaults.standardUserDefaults() returns [String : AnyObject]

We cast it into an NSDictionary. Then by surrounding it in parenthesis '()' will allow us to to call .allKeys or .allValues just as you would on any NSDictionary

 print((UserDefaults.standard.dictionaryRepresentation() as NSDictionary).allKeys)
查看更多
倾城一夜雪
4楼-- · 2019-01-02 19:48

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.

查看更多
无与为乐者.
5楼-- · 2019-01-02 19:54

In Swift we can use the following:-

Swift 3.x & 4.x

For getting all keys & values:

for (key, value) in UserDefaults.standard.dictionaryRepresentation() {
    print("\(key) = \(value) \n")
}

For retrieving the complete dictionary representation of user defaults:

print(Array(UserDefaults.standard.dictionaryRepresentation()))

For retrieving the keys:

// Using dump since the keys are an array of strings.
dump(Array(UserDefaults.standard.dictionaryRepresentation().keys))

For retrieving the values:

We can use dump here as well, but that will return the complete inheritance hierarchy of each element in the values array. If more information about the objects is required, then use dump, else go ahead with the normal print statement.

// dump(Array(UserDefaults.standard.dictionaryRepresentation().values))
print(Array(UserDefaults.standard.dictionaryRepresentation().values))

Swift 2.x

For retrieving the complete dictionary representation of user defaults:

print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation())

For retrieving the keys:

print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array)

For retrieving the values:

print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().values.array)
查看更多
皆成旧梦
6楼-- · 2019-01-02 19:55

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.

查看更多
一个人的天荒地老
7楼-- · 2019-01-02 19:55

Look for the Mac app called SimPholders2. It lives in the menu bar, and lists all of the simulators you've used, and then shows each of your apps. Select one and you get a new Finder window, already open to the app's directory. This makes it super easy to find your app and all of it's directories. It's a huge time saver (and I readily donated to the author).

查看更多
登录 后发表回答