In a previous post we already went into explaining the differences between arrays and sets in Swift, along with their respective use cases. We have also seen examples of how empty and filled sets can be created in Swift using the correct syntax.
Now we will look at some properties and methods that are important for both sets and arrays.
The “.count” property
In Swift, the “.count” property is used to get the number of elements in an array, set or collection.
For example, consider a set called “mySet” containing some unique integers:
var mySet = Set<Int>() mySet.insert(1) mySet.insert(2) mySet.insert(3) let count = mySet.count print(count) // Output: 3
- Here, the “.count” property is used to get the number of elements in the “mySet” set. This will output “3”, which is the number of elements in the set.
Similarly, you can use “.count” with arrays:
let myArray = [1, 2, 3, 4] let count = myArray.count print(count) // Output: 4
- This will output “4”, which is the number of elements in the “myArray” array.
The “.count” property is a convenient way to get the number of elements in an array, set or collection type.
The “.isEmpty” property
In Swift, the “.isEmpty” property is used to check whether an array, set, or collection is empty or not.
Again, consider an empty set called “mySet”:
var mySet = Set<Int>() let isEmpty = mySet.isEmpty print(isEmpty) // Output: true
- Here, the “.isEmpty” property is used to check whether the “mySet” set is empty or not.
- This will output “true”, since the set does not contain any elements.
Similarly, you can use “.isEmpty” with arrays:
let myArray = [1, 2, 3] let isEmpty = myArray.isEmpty print(isEmpty) // Output; false
- This will output “false”, since the “myArray” array is not empty.
The “.isEmpty” property is a convenient way to check whether an array, set, or collection is empty or not .
The “.insert()” method
In Swift, the “.insert()” method is used to add an element to a set or an array.
And again, consider an empty set called “mySet”:
var mySet = Set<Int>() mySet.insert(1) mySet.insert(2) mySet.insert(3) print(mySet) // Output: {3, 1, 2}
- Here, the “.insert()” method is used to add the integer values 1, 2, and 3 to the “mySet” set. This will output “{3, 1, 2}” since sets don’t preserve the order of inserted elements.
Similarly, you can use “.insert()” with arrays:
var myArray = [1, 2, 3] myArray.insert(4, at: 3) print(myArray) // Output; [1, 2, 3, 4]
- This will output “[1, 2, 3, 4]” since the “.insert()” method is used to add the integer value 4 at index 3 of the `myArray` array.
- Note that the “.insert()” method is used with the “at:” parameter to specify the position where the new element should be inserted.
The “.insert()” method is a convenient way to add an element to a set or an array in Swift.
The “.remove()” method
In Swift, the “.remove()” method is used to remove an element from a set or an array.
And again, consider a set called “mySet” containing some integers:
var mySet = Set<Int>() mySet.insert(1) mySet.insert(2) mySet.insert(3) mySet.remove(2) print(mySet) // Output: {1, 3}
- Here, the “.remove()” method is used to remove the integer value 2 from the “mySet” set.
- This will output “{3, 1}” since the element with value 2 has been removed.
Similarly, you can use “.remove()” with arrays:
var myArray = [1, 2, 3] myArray.remove(at: 1) print(myArray) // Output: [1, 3]
- This will output “[1, 3]” since the “.remove()” method is used to remove the element at index 1 of the “myArray” array, which is the value “2”.
- Note that the “.remove()” method is used with the “at:” parameter to specify the position of the element to be removed.
The “.remove()” method is a convenient way to remove an element from a set or an array in Swift.
The “.removeAll()” method
In Swift, the “.removeAll()” method is used to remove all elements from a Set, Array or Collection.
For example, consider a set called “mySet” containing some integers:
var mySet = Set<Int>() mySet.insert(1) mySet.insert(2) mySet.insert(3) mySet.removeAll() print(mySet) // Output: []
- Here, the “.removeAll()” method is used to remove all the elements from “mySet”.
- This will output “[]” since all elements have been removed.
Similarly, you can use “.removeAll()” with arrays:
var myArray = [1, 2, 3] myArray.removeAll() print(myArray) // Output: []
- This will output “[]” since the “.removeAll()” method is used to remove all the elements from the “myArray” array.
The “.removeAll()” method is a convenient way to remove all elements from a Set, Array, or Collection in Swift.
The “.contains()” method
The “.contains()” method in Swift is used to check if a given element is present in a set or an array.
Finally once again, consider a set called “mySet”, and an array called “myArray”:
let mySet = Set(["apple", "banana", "orange"]) let myArray = ["mango", "papaya", "banana"]
- Now, you can use the “.contains()” method to check if a value is present in the set:
let isPresentInSet = mySet.contains("banana") print(isPresentInSet) // Output: true
Similarly, you can use the “.contains()” method to check if a value is present in the array:
let isPresentInArray = myArray.contains("banana") print(isPresentInArray) // Output: true
- In both examples, the “.contains()” method returns a boolean value “true” if the given element is present in the set or array.
- Otherwise it will return “false”
The “.contains()” method is a convenient way to check the presence of an element in a set or an array in Swift.
You may also want to read this:
- How to know about the number of elements in a dictionary in Swift?
- How to remove elements from a dictionary in Swift?
- How to use the “.intersect()”, “.union()”, “symmetricDifference()” and “subtracting()” operations for sets in Swift?
- The “.count” property and the “.append()”, “.insert()” and “.remove()” methods in Swift