Transforming Operators | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429795-reactive-programming-in-ios-with-combine/lessons/7

@ua_learning Your solution is correct in this case because map(_:) replaces all nil values in the array with default ones and force unwraps all optionals in the original stream in just one go and at the very same time using the nil coalescing operator.

The replaceNil(with:) generic method is actually a very particular implementation of the map(_:) one since both prototypes have exactly the same return type: Publishers.Map<Self, T> after all.

The difference between the two signatures is given by the type of the output stream for each one of them.

replaceNil(with:) maps only the nil values of the input stream to default ones and always returns optionals of type T? no matter what, where T is the generic type which the publisher works with.

map(_:) does even more than that by going one step further and converting the original optional type of the publisher to any other custom type of your very own choosing.

You can read even more about both replaceNil(with:) and map(_:) over here:

https://developer.apple.com/documentation/combine/just/replacenil(with:)

https://developer.apple.com/documentation/combine/publisher/map(_:)-99evh

Please let me know if you have any more questions or other issues about the whole thing when you get a chance. Thank you!