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.
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>()
We can also initialize a set with some default elements.
var fruits:Set<String> = ["apple","mango","guava"]
We can set a space pre-allocated for a minimum number/capacity.
var fruits = Set<String>(minimumCapacity: 2)
#xcode #ios #programming #swift #mobile