如何在Swift中从UserDefaults中按套件名称删除所有数据? [英] How to remove all data by a suite name from UserDefaults in Swift?

查看:324
本文介绍了如何在Swift中从UserDefaults中按套件名称删除所有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下名称创建了用户默认值,

I have created user defaults with names below,

let prefs1 = UserDefaults.init(suiteName: "UserAccount")
let prefs2 = UserDefaults.init(suiteName: "UserInfo")

然后,我在这些套件中添加了一些值.即

Then I have added some values to those suites. i.e.

prefs1.set(true, forKey: "Key1")
prefs1.set(true, forKey: "Key2")

prefs2.set(false, forKey: "Key1")
prefs2.set(false, forKey: "Key2")

现在,我要删除prefs1(即名称"UserAccount"),而不要删除prefs2.我尝试过,

Now, I want to remove the prefs1 (i.e. name "UserAccount"), but not prefs2. I tried,

UserDefaults.standard.removePersistentDomain(forName: "UserAccount")

但是它没有移除.我对forNamesuiteName

But it did not remove. I am getting confused with, forName and suiteName

如何从UserDefaults中删除套件,使其他套件在Swift中保持完整?

How to remove a suite from UserDefaults keeping other suites intact in Swift?

推荐答案

func removePersistentDomain(forName domainName: String)

https://developer.apple.com/documentation/foundation/userdefaults /1417339-removepersistentdomain

调用此方法等效于初始化用户默认值 init(suiteName :)传递domainName并调用 在每个键上都使用removeObject(forKey :)方法.

Calling this method is equivalent to initializing a user defaults object with init(suiteName:) passing domainName, and calling the removeObject(forKey:) method on each of its keys.

这篇关于如何在Swift中从UserDefaults中按套件名称删除所有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆