本文介绍了如何快速自动清除NSUserDefault值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何快速快速清除nsuserdefault
值?我已经尝试过了,但这对我不起作用...
How can I auto clear the nsuserdefault
values in swift? I have already tried this but it doesn't work for me...
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
推荐答案
检查已经存储了多少个密钥
check how many keys are already stored
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)
仅添加另一个键
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "justAnotherKey1")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "justAnotherKey2")
检查已再次存储多少个键(+2)
check how many keys are already stored again (+2)
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)
现在创建一个循环以删除键的对象
now create a loop to remove your object for the keys
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不再适用于游乐场,因此需要在真实项目中进行测试)
update: Xcode 7.2.1 • Swift 2.1.1 (note NSUserDefaults doesn't work in playground anymore, so it needs to be tested in a real project)
print(Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys).count)
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "justAnotherKey1")
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)
这篇关于如何快速自动清除NSUserDefault值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!