CocoaAction is a wrapper around
Action type that is available in ReactiveCocoa. (Here you can read more about
Action). We use
CocoaAction to bind our
Actions to GUI controls. Let’s see a quick example of how it works.
Let’s imagine a situation, where we have we have a
UISwitch control. Every time we switch it on or off, we want to change a
MutableProperty text value.
First, let’s create
Action that will take
Bool (switch’s value) as an input, return
String as output and complete without errors.
We have to wrap this action into
CocoaAction that can be bound to
UIControl. In order to do this, we have to remember to transform our
UISwitch's value to
Now we can bind our
UISwitch's value change event. Remember that we have to keep reference to our switchCocoaAction.
Finally, we can observe our
values that are generated by executing our action and change our text value. We can do it quickly by using <~ operator that is available in
This article is cross-posted with my my company blog