---
title: "#162: Reacting with RxSwift ⚗"
url: https://littlebitesofcocoa.com/162-reacting-with-rxswift
author: Jake Marsh
published: 2016-01-05
topics: ["Libraries","Techniques"]
description: "We first learned about reactive-style programming in Bite #127, with ReactiveCocoa. (Give that Bite a quick look first if you're new to the topic)."
---

# #162: Reacting with RxSwift ⚗

By Jake Marsh, January 5, 2016. HTML: https://littlebitesofcocoa.com/162-reacting-with-rxswift

We first learned about reactive-style programming in [Bite #127](https://littlebitesofcocoa.com/127), with [ReactiveCocoa](http://git.io/RAC). (Give [that Bite](https://littlebitesofcocoa.com/127) a quick look first if you're new to the topic).

Today we'll check out an alternative solution called [RxSwift](http://git.io/rxs). It just hit **2.0**, so now is a great time to take a look. Let's dive in.

Before we begin it's important to note that there are [tons](https://github.com/JensRavens/Interstellar) of [great](https://github.com/ReactKit/ReactKit) [libraries](http://git.io/RAC) available for writing this style of code. We can enjoy the benefits of functional and reactive programming with any of them.

[RxSwift](http://git.io/rxs) is another take on reactive/functional programming. Similar to [ReactiveCocoa](https://littlebitesofcocoa.com/127-introduction-to-reactivecocoa), [RxSwift](http://git.io/rxs) works with streams of values called _Observables_. We can <i>subscribe</i> to them, <i>transform</i> them, <i>bind</i> user interface elements to them, and <i>create</i> them.

Additionally, [RxSwift](http://git.io/rxs) ships with _RxCocoa_, a framework that provides extensions for **UIKit** that bring it into [RxSwift's](http://git.io/rxs) reactive world.

Let's check out some examples.

Binding a **UITextField** to a **UILabel**:

``` swift
shipNameField.rx_text
  .map { "Launching \($0)..." }
  .bindTo(statusLabel.rx_text)
```

Creating our own **Observable**:

``` swift
Observable<String>.create {
  $0.onNext("Howdy! 🐴")
  $0.onCompleted()

  return NopDisposable.instance
}
```

Transforming each value change of a **search field** into an API **request**, then updating our UI when it completes:

``` swift
searchBar.rx_text
  .flatMapLatest {
    API.getSearchResults($0)
  }
  .subscribeNext { _ in
    self.tableView.reloadData()
  }
```

[RxSwift](http://git.io/rxs) can transform how we write code in all the layers of our apps. With tight **UIKit** integration from _RxCocoa_, we can now forget about **delegates**, **notifications**, **KVO**, etc and instead use one simple interface to think about how **data** flows through our **app**.

Learn more about _RxSwift_ at [git.io/rxs](http://git.io/rxs)
