iOS NSUserDefaults 删除某项
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"UserInfo"];
[defaults synchronize];//快速保存
iOS NSUserDefaults 删除某项外所有项
// 先将其转化为字典,然后用forin遍历删除即可
NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
NSDictionary *dictionary = [defatluts dictionaryRepresentation];
for(NSString *key in [dictionary allKeys]){
if([key isEqualToString:@"deviceTokenStr"]){//移除deviceTokenStr以外数据
}else{
[defatluts removeObjectForKey:key];
[defatluts synchronize];
}
}
iOS NSUserDefaults 删除所有项
方法1:找到所有的key然后remove掉
/**
* 清除所有的存储本地的数据
*/
- (void)clearAllUserDefaultsData
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dic = [userDefaults dictionaryRepresentation];
for (id key in dic) {
[userDefaults removeObjectForKey:key];
}
[userDefaults synchronize];
}
方法二:清除持久域
/**
* 清除所有的存储本地的数据
*/
- (void)clearAllUserDefaultsData
{
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
}