如何在Swift中从UserDefaults中按套件名称删除所有数据?

编程入门 行业动态 更新时间:2024-10-17 07:23:11
本文介绍了如何在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")

但是它没有移除.我对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中按套件名称删除所有数据?

本文发布于:2023-11-07 10:45:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:套件   名称   数据   如何在   UserDefaults

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!