remove a key-value pair from a map in swift

To remove a key-value pair from a Swift Dictionary (which is essentially a map), you can simply call the removeValue(forKey:) method while passing the key for the particular entry that you want to remove. Here's an example:

main.swift
var myMap = ["apple": 1, "banana": 2, "orange": 3]
myMap.removeValue(forKey: "banana")
print(myMap) // Output: ["orange": 3, "apple": 1]
137 chars
4 lines

In the above example, we have a map named myMap with three entries. We then call the removeValue(forKey:) method while passing the "banana" key, which removes that particular entry from the map. Finally, we print out the resulting map without the "banana" key-value pair.

gistlibby LogSnag