如何删除NSUserDefaults内NSMutableDictionary内的对象

时间:2016-06-30 13:50:41

标签: ios objective-c nsuserdefaults nsmutabledictionary

我正在尝试从userdefaults中的字典中删除对象。

NSMutuableDictionary

上面的代码删除了整个字典。

XYZ是KeyStore msCertStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI"); msCertStore.load(null, null); 。我想删除一个带有键的对象"密码"来自XYZ。

2 个答案:

答案 0 :(得分:3)

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [[defaults objectForKey:@"XYZ"] mutableCopy];
[dict removeObjectForKey:@"Password"];
[defaults setObject:dict forKey:@"XYZ"];

答案 1 :(得分:3)

这应该这样做:

// Access the dictionary you want to edit as a mutable copy
NSMutableDictionary *dict = [[[NSUserDefaults standardUserDefaults] objectForKey:@"dictKey"] mutableCopy];

// Remove the object for the key
[dict removeObjectForKey:@"keyToRemove"];

// Set the dictionary back in user defaults
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"dictKey"];

// Save your changes:
// If you need
// In applicationDidEnterBackground: in iOS >= 7.0
// If you are using iOS < 7.0
[[NSUserDefaults standardUserDefaults] synchronize];