O'Reilly logo

Reactive Programming with Swift 4 by Navdeep Singh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

PublishSubject in action

We can continue to work with the same project that we set up earlier in the chapter. Just use multiline comments to comment out the earlier code to differentiate between outputs from the current sections and the previous ones.

A PublishSubject emits only new items to its subscriber, that is, it does not replay events. We will create a PublishSubject instance named pubSubject:

let pubSubject = PublishSubject<String>()

Note that the PublishSubject initializer is empty, but we need to declare a type that is String in our case. Next, we will subscribe to it and print each emitted event. Subject is empty at this point, so the subscription will not yield anything:

pubSubject.subscribe {        print($0)    }

We will now use the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required