简体   繁体   English

如何在 iPhone 模拟器中重置 NSUserDefaults 数据?

[英]How can I reset the NSUserDefaults data in the iPhone simulator?

I've added NSUserDefaults data retrieval to my app, which is pretty nice.我已经将NSUserDefaults数据检索添加到我的应用程序中,这非常好。 But for testing I would like to reset all the data I added to the defaults database, so that everything is in the state when the user launches the app the first time.但是为了测试,我想重置我添加到默认数据库中的所有数据,以便在用户第一次启动应用程序时一切都处于状态。

I tried to call:我试着打电话:

[NSUserDefaults resetStandardUserDefaults];

but that doesn't do anything.但这没有任何作用。 The defaults are still saved and can be retrieved.默认值仍会保存并可检索。

You want NSUserDefaults removePersistentDomainForName .你想要NSUserDefaults removePersistentDomainForName This will remove all user defaults for the application:这将删除应用程序的所有用户默认值:

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

For more information on the NSUserDefaults class see the Apple docs .有关NSUserDefaults类的更多信息,请参阅Apple 文档

Alternately, if all you are concerned with is data in the iOS Simulator, you can do that via iOS Simulator > Reset Content and Settings .或者,如果您只关心 iOS 模拟器中的数据,则可以通过iOS 模拟器 > 重置内容和设置来实现

在此处输入图片说明

The easiest way is to remove the app from the simulator-- just like you'd remove it from a real phone, by tapping (clicking) and holding until the icons start vibrating.最简单的方法是从模拟器中删除应用程序——就像从真实手机中删除它一样,通过点击(单击)并按住直到图标开始振动。 That removes all app data, and the next time you install from Xcode it's like the first time.这会删除所有应用程序数据,下次从 Xcode 安装时就像第一次一样。

If you have other app data you need to keep, you have a couple of options.如果您有其他需要保留的应用数据,您有几个选择。

One way would be to have some debug code that calls removeObjectForKey: on each of your defaults keys.一种方法是使用一些调试代码调用 removeObjectForKey: 在每个默认键上。

The other is to find the directory where the simulator copy is installed, and remove the file containing the preferences.另一种是找到模拟器副本的安装目录,删除包含首选项的文件。 Use this to find the app:使用它来查找应用程序:

ls -ld ~/Library/Application\ Support/iPhone\ Simulator/User/Applications/*/*.app

The full path to your app will contain directory whose name is a UUID.应用程序的完整路径将包含名称为 UUID 的目录。 In that directory, look in Library/Preferences for the preferences file.在该目录中,在 Library/Preferences 中查找首选项文件。 Remove that, and user preferences are gone.删除它,用户首选项就消失了。

您可能会发现您为应用程序的用户默认设置“写入”的内容都在文件中,删除此 .plist 文件有效:

user name/Library/Preferences/com.theAppYouAreHandling.plist

在 Swift 2.0 中,以下 1 行将重置应用程序的所有 NSUserDefaults:

NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!)

实际上,这可能并不适用于所有情况,但由于我在模拟器中没有保留任何有价值的信息,我只是从模拟器本身的 iPhone 菜单中重置内容和设置。

Til Xcode 6 and iOS 8 Simulator the location of the plist file changed.直到 Xcode 6 和 iOS 8 Simulator plist 文件的位置发生了变化。

I found the *.plist in the following directory:我在以下目录中找到了 *.plist:

[1] /Users/SOME-USERNAME/Library/Developer/CoreSimulator/Devices/SOME-DEVICE-ID/data/Library/Preferences/SP.UserDefaultsTest.plist [1] /Users/SOME-USERNAME/Library/Developer/CoreSimulator/Devices/SOME-DEVICE-ID/data/Library/Preferences/SP.UserDefaultsTest.plist

Deleting the located file from path [1] manually and the NSUserDefaults are gone.手动从路径 [1] 中删除定位的文件,NSUserDefaults 消失了。

Here's the swift version:这是快速版本:

let domainName = NSBundle.mainBundle().bundleIdentifier!       
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(domainName)

在模拟器顶部菜单中:

Simulator -> Reset Content and Settings...

You can use removePersistentDomainForName method available with NSUserDefaults Class.您可以使用 NSUserDefaults 类提供的removePersistentDomainForName方法。

Syntax :句法 :

- (void)removePersistentDomainForName:(NSString *)domainName

Example :例子 :

NSString *strAppBundleId = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:strAppBundleId];

If you're doing this in a unittest, you may want to keep the state of your app in the current simulator instead of inadvertently wiping it every time you also run your unittests.如果您在单元测试中执行此操作,您可能希望将应用程序的状态保留在当前模拟器中,而不是在每次运行单元测试时无意中擦除它。 One way to do this is to simply hang on to the old values for your app domain in setUp() and then restore them in tearDown() :一种方法是简单地在setUp()保留应用程序域的旧值,然后在tearDown()恢复它们:

class MyClass_Tests: XCTestCase {
    static let domainName = Bundle.main.bundleIdentifier!
    static var oldUserDefaults: [String : Any]?

    override class func setUp() {
        super.setUp()
        // Hang onto this so we don't inadvertently wipe the app's state while running tests.
        oldUserDefaults = UserDefaults.standard.persistentDomain(forName: domainName)
    }

    override class func tearDown() {
        // Restore the old values.
        UserDefaults.standard.setPersistentDomain(oldUserDefaults!, forName: domainName)
        super.tearDown()
    }

    override func setUp() {
        super.setUp()
        // Wipe the state for each test.
        UserDefaults.standard.removePersistentDomain(forName: MyClass_Tests.domainName)
    }

    override func tearDown() {
        super.tearDown()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM