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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/898498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:56:30  来源:igfitidea点击:

How can I reset the NSUserDefaults data in the iPhone simulator?

iphoneioscocoa-touchuikitnsuserdefaults

提问by Thanks

I've added NSUserDefault data retrieval to my app, which is pretty nice. 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.

我已经将 NSUserDefault 数据检索添加到我的应用程序中,这非常好。但是为了测试,我想重置我添加到默认数据库中的所有数据,以便在用户第一次启动应用程序时一切都处于状态。

I tried to call:

我试着打电话:

[NSUserDefaults resetStandardUserDefaults];

[NSUserDefaults resetStandardUserDefaults];

but that doesn't do anything. The defaults are still saved and can be retrieved.

但这没有任何作用。默认值仍会保存并可检索。

采纳答案by Tom Harrington

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. In that directory, look in Library/Preferences for the preferences file. Remove that, and user preferences are gone.

应用程序的完整路径将包含名称为 UUID 的目录。在该目录中,在 Library/Preferences 中查找首选项文件。删除它,用户首选项就消失了。

回答by memmons

You want NSUserDefaults removePersistentDomainForName. This will remove all user defaults for the application:

你要NSUserDefaults removePersistentDomainForName。这将删除应用程序的所有用户默认值:

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

For more information on the NSUserDefaultsclass 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 模拟器 > 重置内容和设置来实现

enter image description here

在此处输入图片说明

回答by Jiulong Zhao

You may find out what you have "written" to userdefaults for the app are all inside a file delete this .plist file works:

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

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

回答by dane

In Swift 2.0 the following 1 line will reset all the NSUserDefaults for an app:

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

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

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

回答by mmc

Actually, this may not be suitable in every circumstance, but since I keep no information of value in the simulator, I just Reset Content and Settings from the iPhone menu, from within the simulator itself.

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

回答by sys4tron

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 消失了。

回答by Ankit Goel

Here's the swift version:

这是快速版本:

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

回答by Andrey

In the simulator top menu:

在模拟器顶部菜单中:

Simulator -> Reset Content and Settings...

回答by qix

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()
    }
}

回答by Jayprakash Dubey

You can use removePersistentDomainForNamemethod available with NSUserDefaults Class.

您可以使用NSUserDefaults 类提供的removePersistentDomainForName方法。

Syntax :

句法 :

- (void)removePersistentDomainForName:(NSString *)domainName

Example :

例子 :

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