Swift Combine: prepend() operator

In this tutorial, you are going to look at prepend() operator. Just as the name implies, the prepend() operator is going to append element in your sequence.

A publisher

I created a range from 1 to 5 and made it a publisher. You can see 1,2,3,4,5 are printed out. But what if I want to prepend a value before the sequence happens?

prepend()

When you use this .prepend() operator, you can prepend some element. You can actually pass in a number or numbers to prepend.

Since you are using combination operators and the reactive way of writing your code, you can also chain different operator.

Not only you can provide a variadic parameter (sending multiple values in the argument), you can pass in an array.

Combining Publisher with prepend()

Besides numbers, I created another publisher from 500 to 510. You can pass in a publisher. You can check the result.

Conclusion

  • prepend() allows you to add elements before the sequence.
  • You can pass in variadic parameters , arrays and publisher as a parameter.

--

--

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!