Removing pairs

Bob — er, sorry — Bobby, still doesn’t feel safe, and he wants you to remove all information about his whereabouts:

bobData.removeValue(forKey: "state")

This method will remove the key state and its associated value from the dictionary. As you might expect, there’s a shorter way to do this using subscripting:

bobData["city"] = nil

Assigning nil as a key’s associated value removes the pair from the dictionary.

Note: If you are using a dictionary that has values that are optional types, dictionary[key] = nil still removes the key completely. If you want keep the key and set the value to nil you must use the updateValue method.

results matching ""

    No results matching ""