Sets are basically a collection of items that aren’t ordered but are unique. Let’s go over different operations/functions that Swift has when it comes to Sets.

Creating a Set

Initialize an empty set

Sets in Swift are of a generic nature, so they can hold any type if it conforms to the Hashable Protocol. For the sake of simplicity, we will use String and Int.

var fruits = Set<String>()

Initialize a set with elements

We can also initialize a set with some default elements.

var fruits:Set<String> = ["apple","mango","guava"]

Initialize a set with a capacity

We can set a space pre-allocated for a minimum number/capacity.

var fruits = Set<String>(minimumCapacity: 2)

#xcode #ios #programming #swift #mobile

How to Use Sets in Swift
1.05 GEEK