Reactivity on Apple's platforms

In ReactiveCocoa 2 (including versions 3 and 4), reactivity for Apple's platforms (UIKit and AppKit frameworks) was tucked away under prefix methods (e.g. rac_newTextSignal).

In version 5 this has been changed and all of the reactivity has been nicely tucked away under the .reactive member of UIKit and AppKit classes.

Let's look at couple of examples how we can use this in iOS application.

First a simple greeting application. A new project from a standard "One View Application" template with one label and one textfield would be more than enough for the demonstration.

Here's how I setup my single view controller scene:

A scene with text field and two labels

The text field is connected through an outlet to a view controller as well as label below it.

Here is how code would have looked before version 5:

override func viewDidLoad() {  

    label.rac_text <~ textField.rac_newTextSignal()
                                  .map { $0 as! String }

That's simple code to channel the input text from UITextField to UILabel. A bit much (mostly because of the legacy API). Here's how it looks in version 5:

override func viewDidLoad() {  

    label.reactive.text <~ textField.reactive.continuousTextValues

Quite the improvement over three split lines! If you run and compile it you will get the label below the text field update every-time you type something.

I encourage you to browse through ReactiveCocoa source code to see what reactive extensions are available.