NSUserDefaults removeObjectForKey vs. setObject:nil

Nsuserdefaults

Nsuserdefaults Problem Overview


Are the following two lines equivalent?

  1. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"example key"]

  2. [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"example key"]

Nsuserdefaults Solutions


Solution 1 - Nsuserdefaults

Yes, both lines of code are equivalent, both will result in nil read

id obj = [[NSUserDefaults standardUserDefaults] objectForKey:@"example key"];

NSUserDefaults will return nil if the key was not found. I would recommend to use the removeObjectForKey instead of setting it to nil.

here is how to test if setting key value to nil removed the key entry from NSUserDefaults standardUserDefaults.

NSArray *keys = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] copy];
   for(NSString *key in keys) {
       NSLog(@"Key Name: %@", key);
}
[keys release];

or simply dump the key/value dictionary of NSUserDefaults standardUserDefaults

NSLog(@"All contents of NSUserDefaults: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

Solution 2 - Nsuserdefaults

Swift 3.0

The below answer is no longer the case when I tested this. When set to nil the result is NSCFData being stored. Possibly an NSNull object reference, but I am not positive.

To completely remove a value for a key use UserDefaults.standard.removeObject(forKey: "YourDefault")

I tested with the following code:

UserDefaults.standard.set(["a", "b", "c"], forKey: "MyDefaults")
print("Test A: My saved defaults \(UserDefaults.standard.object(forKey: "MyDefaults"))")
    
UserDefaults.standard.set(nil, forKey: "MyDefaults")
print("Test B: My defaults set to nil \(UserDefaults.standard.object(forKey: "MyDefaults"))")
    
UserDefaults.standard.removeObject(forKey: "MyDefaults")
print("Test C: My defaults removed \(UserDefaults.standard.object(forKey: "MyDefaults"))")

Solution 3 - Nsuserdefaults

Swift 5.0 + iOS 11 and up

Both methods remove the value. Tried this in a playground:

import Foundation

let key = "Test"
let value = "test"
let defaults = UserDefaults.standard

func printUD() {
    print("UserDefaults after modification:\n")
    defaults.dictionaryRepresentation().forEach { print("\($0): \($1)\n") }
    print("-------------\n\n")
}

defaults.set(value, forKey: key); printUD()
defaults.set(nil, forKey: key); printUD()
defaults.set(value, forKey: key); printUD()
defaults.removeObject(forKey: key); printUD()

Prior to iOS 11 this will result in serializing nil into Data and throw an error.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionma11hew28View Question on Stackoverflow
Solution 1 - NsuserdefaultsRocketManView Answer on Stackoverflow
Solution 2 - NsuserdefaultsSeanView Answer on Stackoverflow
Solution 3 - NsuserdefaultsDaniil KorotinView Answer on Stackoverflow