Swift Combine: last() operator. Unknown but useful operator.

Let’s learn about the last() operator. You can pretty much guess the last operator is going to find the the last value in the sequence that satisfies a particular condition that is set by you.

find the last element using last()

I created numbers with range from 1 to 9 and made it a publisher. You can clearly see that last() operator takes a closure where you must define your condition. Here in this example, I wanted to find the last even number in the sequence.

This is the whole purpose of last() operator. It is going to wait until the last value has been checked and then simply return the value which satisfies your condition.

Conclusion

  • last() operator searches for the entire sequence to find the last element that meets your criteria.

--

--

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!