A set is empty if it has no element. In Swift, we can create an empty set very simply.
setName = Set<dataType>()
setName
: This is the name of the empty set that we want to create.dataType
: This represents the type of data the empty element should have.A set of a specified data type that is empty is returned.
import Swift// create empty setslet intSet = Set<Int>()let stringSet = Set<String>()let boolSet = Set<Bool>()let floatSet = Set<Float>()// create a set that is not emptylet evenNumbers: Set<Int> = [2, 4, 6, 8, 10]// print setsprint(intSet)print(stringSet)print(boolSet)print(floatSet)print(evenNumbers)// check if they are emptyprint(intSet.isEmpty)print(stringSet.isEmpty)print(boolSet.isEmpty)print(floatSet.isEmpty)print(evenNumbers.isEmpty)
isEmpty
method, which returns true
if a set is empty, We check if the sets are empty and print the values to the console.