O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

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

An example

Let's suppose that we have an outlet and we want to observe its changes:

@IBOutlet weak var textFieldUserName: UITextField! 

We can create a SignalProducer as follows:

let userNameSignalProducer = textFieldUserName.rac_textSignal().toSignalProducer.map {     text in     text as! String } 

The rac_textSignal method is a ReactiveCocoa extension for UITextField that can be used to create the signal producer.

Then, we can start our SignalProducer as follows:

userNameSignalProducer.startWithNext {      results in     print("User name:\(results)") } 

This will print any changes in our textField to the console.

Also, we can execute operations such as map, flatMap, filter, and reduce on this signal producer, which we covered in Chapter 6, Map, Filter, ...

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