We use the popFirst()
method in Swift to remove and return the first element of a set. It is important to remember that a set in Swift is unordered, so the first element may not be the first element on the set as we see it.
set.popFirst()
This method does not take any parameter values.
This method returns the first element of the set that was removed.
// create some setsvar names: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]var workers: Set = ["Alicia", "Bethany", "Diana"]var numbers: Set = [1, 2, 3, 4, 5, 7, 8, 9, 10]// pop and return first elementprint(names.popFirst()!)print(workers.popFirst()!)print(numbers.popFirst()!)
popFirst()
method. Then, we return the popped or removed element.