Modifying values in a set

suggest change
var favoriteColors: Set = ["Red", "Blue", "Green"]
//favoriteColors = {"Blue", "Green", "Red"}

You can use the insert(_:) method to add a new item into a set.

favoriteColors.insert("Orange")
//favoriteColors = {"Red", "Green", "Orange", "Blue"}

You can use the remove(_:) method to remove an item from a set. It returns an optional containing the removed value, or nil if the value was not in the set. Set order is not guaranteed; the comments show membership, not a required order.

let removedColor = favoriteColors.remove("Red")
//favoriteColors = {"Green", "Orange", "Blue"}
// removedColor = Optional("Red")

let anotherRemovedColor = favoriteColors.remove("Black")
// anotherRemovedColor = nil

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents