首页 > 解决方案 > 无法删除 NSUserDefaults (iOS) 中的键对象

问题描述

由于某些奇怪的原因,我无法删除 NSUserDefaults 中的某些键。我在做:

UserDefaults.standard.removeObject(forKey: "currentSelectedCategoryID")
print(UserDefaults.standard.string(forKey: "currentSelectedCategoryID"))

"58707"当我打印该值时,它显示的值与删除对象之前完全相同。我什至尝试过UserDefaults.standard.set(nil, forKey: "currentSelectedCategoryID"),它正在做同样的事情。

为什么我不能删除我的密钥?

标签: iosswiftnsuserdefaults

解决方案


我想到了。我正在与我的共享扩展共享我的用户默认值,并且我没有删除该用户默认对象的密钥。

UserDefaults.standard.removeObject(forKey: "currentSelectedCategoryID")
if let shared = UserDefaults(suiteName: {YOUR SUITE NAME}) {
     shared.removeObject(forKey: currentSelectedCategoryID)
}

推荐阅读