Easy way to see saved NSUserDefaults?

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


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/<Sim Version>/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.


You can print all current NSUserDefaults to the log:

Just keys:

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

Keys and values:

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

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)
链接地址: http://www.djcxy.com/p/85166.html

上一篇: 如何在NSUserDefaults中存储自定义对象

下一篇: 简单的方法来查看保存的NSUserDefaults?