I’m learning Swift and SwiftUI, and as part of this journey I’m working my way through Donny Wals’ excellent Practical Combine: An introduction to Combine with real examples!

The problem

I’ve been working on my 11” iPad Pro running iPad OS 13.5.1, with the book open on one half of the screen and Swift Playgrounds on the other, and I’ve noticed that the results from calls to print in Combine callbacks are not output on my iPad:

A Workaround

I worked around this by just entering the variable name:

Another problem

This feels like a bug to me, but I had a good workaround. Until, that is, I wanted to use the Combine print function. I needed to see that output.

This is what Donny’s book showed, and what my iPad Swift Playground showed: Combine print showing nothing

The console output was lost for the prints both in the reactive chain and in the sink. Googling gave me no obvious answers.

When learning, this is exactly the kind of thing I like to dig into, and try solve.

A solution

This is the solution I came up with:

import Combine

class PlaygroundConsole : TextOutputStream, CustomStringConvertible {
    var text = String()
    func write(_ string: String) {
    var description : String { text}
let console = PlaygroundConsole()

// Based on https://stackoverflow.com/a/47223166
func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
    let output = items.map { "\($0)" }.joined(separator: separator)
    console.write(output + terminator)

You can find the latest version of this at https://github.com/DamianMehers/playgroundconsole

I added a TextOutputStream which I could pass to the Combine print and I shadowed the global print function to write to the same stream. The implementation simply stores up the text, and it can then be output at the end.

If I change the Combine print to send it’s output to the PlaygroundConsole and then examine the console variable’s content at the end, this is what I see:

Note the modified .print(to: console) in the Combine chain.

Disclaimer: I’m new around here

As I said, I am new to Swift. This is a very simple solution, but hopefully it will help someone else too.

This could already be a solved problem, and I didn’t stumble upon it when Googling. Also, the above code is newbie code - feel free to let me know of any mistakes or non-idiomatic code (I’ve been doing C# for many years).

In any event, if you are also learning Swift and SwiftUI and want to start from the beginning using Apple’s Combine framework, Practical Combine is a great way to learn.

You can find the latest version of the console code at https://github.com/DamianMehers/playgroundconsole