<commit_before>---
title: RxSwift For Dummies 🐤 Part 3
---

Let's learn about the next building block of RxSwift: **Subjects**

We've talked about the `Observable`, right? I think we can all agree that when you have a reference to an `Observable` it's *output* only. You can subscribe to it's *output*, but you can't change it.

A `Subject` is an *output*(`Observable`) but it's also an **input**! That means that you can **dynamically**/**imperatively** emit new elements in a sequence.

{% highlight swift %}
let subject = PublishSubject<String>()

// As you can see the subject casts nicely, because it's an Observable subclass
let observable : Observable<String> = subject

observable
    .subscribe(onNext: { text in
        print(text)
    })
    .addDisposableTo(disposeBag)

// You can call onNext any time you want to emit a new item in the sequence
subject.onNext("Hey!")
subject.onNext("I'm back!")
{% endhighlight %}

`onNext` is the method you use to do the *input*.

It will of course print:

{% highlight text %}
"Hey!"
"I'm back!"
{% endhighlight %}

Why do we need subjects? To easily connect the declarative/RxSwift world with the imperative/normal world. Subjects feel more "natural" to programmers used to imperative programming. 

In a perfect, clean RxSwift implementation you [are discouraged to use subjects](https://github.com/ReactiveX/RxSwift/issues/487) - you should have a perfect stream of observables. Let's not bother our heads with it though, I'll explain that in a separate post, please use as much subjects as you want for now 😅

So that's basically it! Let's learn to control subjects now.

### Hot🔥 vs Cold❄️

I foreshadowed it in the first part of the tutorial. Now it's important to grasp, because subjects are the first hot observables we're encountering.

As we already established, when you create/declare an `Observable` using `create` it won't execute until there's an observer that observes on it. It'll start execution at the same moment something calls `subscribe` on it. That's why it's called a cold❄️ observable. If you don't remember you can take a quick look at [Part 1](http://swiftpearls.com/RxSwift-for-dummies-1-Observables.html)<commit_msg>no message<commit_after>---
title: RxSwift For Dummies 🐤 Part 3
---

Let's learn about the next building block of RxSwift: **Subjects**

I think we can agree that when you have a reference to an `Observable` it's *output* only. You can subscribe to it's *output*, but you can't change it.

A `Subject` is an *output*(`Observable`) but it's also an **input**! That means that you can **dynamically**/**imperatively** emit new elements in a sequence.

{% highlight swift %}
let subject = PublishSubject<String>()

// As you can see the subject casts nicely, because it's an Observable subclass
let observable : Observable<String> = subject

observable
    .subscribe(onNext: { text in
        print(text)
    })
    .addDisposableTo(disposeBag)

// You can call onNext any time you want to emit a new item in the sequence
subject.onNext("Hey!")
subject.onNext("I'm back!")
{% endhighlight %}

`onNext` is the method you use to do the *input*.

It will of course print:

{% highlight text %}
"Hey!"
"I'm back!"
{% endhighlight %}

Why do we need subjects? To easily connect the declarative/RxSwift world with the imperative/normal world. Subjects feel more "natural" to programmers used to imperative programming. 

In a perfect, clean RxSwift implementation you [are discouraged to use subjects](https://github.com/ReactiveX/RxSwift/issues/487) - you should have a perfect stream of observables. Let's not bother our heads with it though, I'll explain that in a separate post, please use as much subjects as you want for now 😅

So that's basically it! Let's learn to control subjects now.

### Hot🔥 vs Cold❄️

I mentioned the Hot🔥 vs Cold❄️ in the first part of the tutorial. Let's get into it in more detail now, because subjects are actually the first hot observables we're encountering.

As we already established, when you create/declare an `Observable` using `create` it won't execute until there's an observer that observes on it. It'll start execution at the same moment something calls `subscribe` on it. That's why it's called a cold❄️ observable. If you don't remember you can take a quick look at [Part 1](http://swiftpearls.com/RxSwift-for-dummies-1-Observables.html)
{'fraction_non_alphanumeric': 0.09144947416552354, 'fraction_numerical': 0.0027434842249657062, 'mean_word_length': 4.34841075794621, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '130752', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1205, 'n_words': 612}