加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何自动清除swift中的NSUserDefault值?

发布时间:2020-12-14 05:53:49 所属栏目:百科 来源:网络整理
导读:参见英文答案 Delete all keys from a NSUserDefaults dictionary iOs13个答案如何自动清除swift中的nsuserdefault值?我已经尝试过了,但对我来说并不奏效 [[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:
参见英文答案 > Delete all keys from a NSUserDefaults dictionary iOs13个答案如何自动清除swift中的nsuserdefault值?我已经尝试过了,但对我来说并不奏效
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
检查已存储了多少个密钥
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)

添加另一个关键

NSUserDefaults.standardUserDefaults().setBool(true,forKey: "justAnotherKey1")
NSUserDefaults.standardUserDefaults().setBool(true,forKey: "justAnotherKey2")

检查已经存储了多少个密钥(2)

print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)

现在创建一个循环来删除键的对象

for key in NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys {
    NSUserDefaults.standardUserDefaults().removeObjectForKey(key.description)
}

检查你有多少钥匙

print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)

更新:Xcode 7.2.1?Swift 2.1.1(注意NSUserDefaults不再在操场中运行,因此需要在真实项目中进行测试)

print(Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys).count)

NSUserDefaults.standardUserDefaults().setBool(true,forKey: "justAnotherKey2")

print(Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys).count)

for key in Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys) {
    NSUserDefaults.standardUserDefaults().removeObjectForKey(key)
}

print(Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys).count)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读