ReactiveCocoa Usage and Macros

Common Usage As mentioned earlier, RAC can replace delegates, KVO, and more. Let's look at the specific usage. Replacing Delegates: - : Used to replace delegates. - How it works: It monitors whether a method has been called. When it is called, a signal is automatically emitted. - Use case: Custom — listen for button taps inside the custom view. - Previously, you would need a delegate property on the custom view and call the delegate when the button is tapped. - : Converts the invocation of a method on an object into a signal. Whenever that method is called, the signal fires. - In this example, whenever calls , a signal is emitted — just subscribe to it. Code: Replacing KVO: - : Used to monitor property changes on an object. - The receiver: the object being observed. - : the property to observe. - Converts changes to 's property into a signal. Whenever the value changes, the signal fires. - : can be . Listening to Events: - : Used to listen for a specific control event. - Converts a button tap event into a signal. When the button is tapped, the signal fires. Replacing Notifications: - : Used to listen for a specific notification. - Whenever the notification is posted, it converts into a signal. Listening to Text Field Changes: - : Emits a signal whenever the text field's content changes. - Gets the signal for text field text changes. - Use case: When an interface has multiple requests, you want to wait until all data has been received before displaying the UI. - — when all signals in the array have each called at least once, the first parameter's method is triggered. - Usage note: the number of signals must match the number of parameters in the selector method; each parameter corresponds to the value emitted by its respective signal. - You don't need to manually subscribe to and — the method handles that internally. Common RAC Macros - Used to bind a property of an object. Binds a signal to an object's property — whenever the signal emits a value, the property is assigned that value. - The example below keeps the label's text in sync with the text field's content. - Used to bind to a property of an object. - Quickly observe changes to a specific property on an object. - Returns a signal that fires whenever that property changes. RACTuplePack and RACTupleUnpack - : Wraps data into a . Place the types to pack as macro parameters and they are automatically wrapped. - : Unpacks a into the corresponding data. The right side of the equals sign is the tuple to unpack. The macro's parameters specify the types to unpack into. Code: All code from this article can be found on my GitHub at .