Swift Combine: append() operator
Do you know prepend()
operator? prepend()
operator allows you to add additional elements before the sequence. But append()
operator is adding new elements at the end of the operator.
append()
let numbers = (1...10).publishernumbers
.append(99, 100)
.sink {
print($0) // 1,2,3,4,5,6,7,8,9,10,99,101
}numbers
.append([99, 100])
.sink {
print($0) // 1,2,3,4,5,6,7,8,9,10,99,101
}
Let’s take an example. I created numbers range from 1 to 10 and made it a publisher.
You can see that you can either pass variadic parameter or an array and appended values appear at the end of the array,
Passing in publisher
let numbers = (1...10).publisher
let publisher2 = (11...20).publishernumbers
.append(publisher2)
.sink {
print($0) // 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
}
In this example, I passed in publisher2 and you can check the result. It’s successfully added.
Conclusion
append()
operator adds elements at the end of the array.append()
operator takes invariadic
,array
andpublisher
as an argument.