Swift Combine CompactMap vs flatMap

Mario tsai
Aug 8, 2021

--

map vs compactMap vs flatMap

First We see the code

map battle

CompactMap filter ‘nil’

And you use .publish. it will send the value one by one.

But you want replace “nil” to some value. Try this one.

FlatMap is more

ps. flatMap in Combine, the error type of the new publisher must match that of the source publisher.

Next one for more flatMap ~~!

happy coding

--

--

Mario tsai

Major in physical education. Football referee. And high interest in looking the world through the numbers.