This is Understanding Combine, written by Matt Neuburg. It is a work in progress. Corrections and suggestions are greatly appreciated (you can comment here). So are donations; please consider keeping me going by funding this work at http://www.paypal.me/mattneub. Or buy my books: the current editions are iOS 13 Programming Fundamentals with Swift and Programming iOS 13. Thank you!


Debuggers

Debuggers
By debuggers I mean those operators that are useful primarily when you’re debugging your pipeline.

The debugger operator par excellence is .print, which prints to the console all messages passing up and down the pipeline through the operator.

One of the hardest aspects of using Combine is simply knowing what output and failure types are passing the pipeline at any point. Oddly, there is no built-in debugger operator that helps with this. However, here’s a good trick: start entering a .handleEvents call within the pipeline, and let code completion enter the template for the call. The template for the receiveOutput: parameter tells you the Output (value) type at that point, and the template for the receiveCompletion: parameter tells you the Failure (error) type at that point. (You can then select and remove the template for the call; you’ve found out what you wanted to know.)

Another way to learn the type of something is to assign it to a local variable, select the variable, and look in the Quick Help inspector. It can be useful to construct your pipeline in stages, assigning the part of the pipeline created so far to a local variable, just so that you can use this trick. Similarly, within an operator’s function, you can assign (say) $0 to a local variable so that you can learn the type of the incoming parameter. Again, you can remove these local variables when you no longer need them.


Table of Contents