The prefix()
method is used on a set, and it returns a sub-sequence up to the specified length with the initial elements of the particular set.
set.prefix(length)
The prefix()
method takes length
as a parameter that represents the number of elements of a sub-sequence, we want to return.
This method returns a sub-sequence.
Let's look at the code below:
// create some setslet names: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]let numbers: Set = [1, 2, 3, 4, 5, 7, 8, 9, 10]// get some sub-sequence using the prefix() methodvar prefix1 = names.prefix(2)var prefix2 = numbers.prefix(5)// print each elementfor each in prefix1{print(each)}// print each elementfor each in prefix2{print(each)}
prefix()
method. Then, we store this sub-sequence in variables prefix1
and prefix2
.for-in
loop to print each element of the sub-sequences.