NSUserDefaults清除全部或某个的储存数据

/* 清除所有的存储本地的数据*/

//方法一:清除每个Key

[userDefaults    removeObjectForKey:@""];

[userDefaults   synchronize];
-(void)clearAllUserDefaultsData{
    
    NSString * appDomain = [[NSBundle mainBundle] bundleIdentifier];
    
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
    
}
// 方法二:清除持久域
-(void)clearAllUserDefaultsData2{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSDictionary *dic = [userDefaults dictionaryRepresentation];
    
    for(id key in dic){
        [userDefaults removeObjectForKey:key];
    }
    [userDefaults synchronize];
}

2.清除某个Key

[userDefaults    removeObjectForKey:@""];

[userDefaults   synchronize];

你可能感兴趣的:(NSUserDefaults清除全部或某个的储存数据)