我用以下名称创建了用户默认值,
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")但是它没有移除.我对forName和suiteName
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)
developer.apple/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中按套件名称删除所有数据?
发布评论