Name: rac-example-objc
Owner: Futurice
Description: Example RAC playground for iOS, written in Objective-C
Created: 2016-04-29 09:08:54.0
Updated: 2016-04-29 09:21:20.0
Pushed: 2016-05-11 04:58:01.0
Homepage: null
Size: 223
Language: Objective-C
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
ReactiveCocoa (RAC) is a powerful framework for working with streams of values over time. It has already been added to the project workspace and is ready to use.
Observables are called signals in RAC. You can create one using RACSignal
's class method createSignal
, which gives you a block from which you can send values and other events to subscribers. You need to return a RACDisposable
, which allows you to clean up things once a subscription ends.
RAC comes with a range of categories on UIKit, which let you access properties such as UITextField
values as streams. Every time the value changes, the stream sends a Next
event carrying that value. These category methods all start with the rac_
prefix.
Once you have a signal that sends the text field's latest values, you can bind it to the label using the RAC
macro:
self.label, text) = textSignal;
This also automatically terminates the subscription once the label gets deallocated.
These operators include map
for transforming values, and filter
for skipping values based on a condition.
For instance, try converting the text field values to uppercase before displaying them in the label.
RAC comes with useful logging features, such as the doNext
operator. It lets us print the values to the console in a non-invasive fashion (without subscribing to the signal). Note that a subscription is still required at the end of the chain, or you won't see anything!
The aim is to let the user control what they see in the label, using the segmented control. Depending on their choice, either the latest text field input or the current date is shown.
0
or 1
for the first and second segment, respectively) to the signals you want to switch between.switchToLatest
operator to forward only values from latest returned “inner” signal.The date signal sends a value every second, meaning that the label text won't update immediately when the user switches to it. We can fix this by appending replayLast
to the date signal, which causes it to send its last value to new subscribers immediately upon subscription.