.remove()

The .remove() method removes and returns a specified element from a set.

Syntax

setName.remove(at: element)

If the element parameter exists in setName, it is removed and returned.

Example

In the example below, The "autumn" element in the weather set is removed:

var weather: Set = ["spring", "summer", "autumn", "fall", "winter"]
let toRemove = "autumn"
if let _ = weather.remove(toRemove) {
print(weather)
}

This will output the following:

["spring", "summer", "fall", "winter"]

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Swift on Codecademy

Contributors