In Swift, you can check whether a set contains a specific element or not by using the contains()
method. Its syntax is as follows:
set.contains(element)
Where set
is the name of the set you want to check and element
is the value you want to look for. The method returns a Boolean value of true
or false
.
Example:
let mySet: Set<String> = ["Sling", "Academy", "Swift", "iOS"]
// check if mySet contains "Swift"
if mySet.contains("Swift") {
print("mySet contains 'Swift'")
} else {
print("mySet does not contain 'Swift'")
}
Output:
mySet contains 'Swift'
This succinct tutorial ends here. Happy coding!