Swift Language Sets Modifying values in a set

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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 optional containing value that was removed or nil if value was not in the set.

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

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


Got any Swift Language Question?