What is set.removeFirst() method in Swift?
Overview
The removeFirst() method in Swift is used to remove the first element of a set. The order of a set is different. The first element that appears in the first position of a set is not necessarily the first element.
Syntax
set.removeFirst()
Syntax for removeFirst() method of a Set
Parameter
This method doesn't take any parameters.
Return value
Thi method returns the first removed element.
Code example
Let's look at the code below:
// create some setsvar names: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]var workers: Set = ["Dorothy", "Stephen", "Jane"]var numbers: Set = [1, 2, 3, 4, 5, 7, 8, 9, 10]// pop and return first elementprint(names.removeFirst())print(names.removeFirst())print(workers.removeFirst())print(numbers.removeFirst())
Explanation
- Lines 2 to 4: We create some set instances and initialize them with some elements.
- Lines 7 to 10: We use the
removeFirst()method to remove the first elements of the sets and print to the console.