There are several options for integrating RxSwift into a project, including CocoaPods, Carthage, and the Swift Package Manager. In this series, we use CocoaPods. You can find installation instructions for Carthage and the Swift Package Manager on GitHub.

Installing RxSwift and RxCocoa with CocoaPods is easy. Open Xcode and create a new project by choosing the Single View App template from the iOS > Application section.

Project Setup

Name the project RxExample and make sure the checkboxes at the bottom are unchecked.

Project Setup

We first install the dependencies of the project. Close the project, open a Terminal window, and navigate to the root of the project. We set up CocoaPods by executing the pod init command. This command creates a Podfile, which you can find at the root of the project.

pod init

Open the Podfile in a text editor, set platform to iOS 13.0, and add RxSwift and RxCocoa to the Podfile.

platform :ios, '13.0'

target 'RxExample' do
  use_frameworks!

  pod 'RxSwift'
  pod 'RxCocoa'
end

Return to the Terminal window and run the pod install command to install the project's dependencies.

pod install

Navigate to the root of the project and open the workspace CocoaPods has created for us. Choose Build from the Product menu to build the project and its dependencies.

Right-click the project in the Project Navigator on the left and choose New File.... Select Blank from the list of templates in the iOS > Playground section and name the playground Reactive.playground.

Playground Setup

Clear the contents of the playground and add an import statement for RxSwift and Foundation.

import RxSwift
import Foundation

To make sure everything is working, we create an observable and emit a few events. We define a constant, subject, and instantiate an instance of the PublishSubject class. Don't try to understand the code we are adding. We cover subjects later in this series.

import RxSwift
import Foundation

let subject = PublishSubject<Int>()

We subscribe to the observable of the subject by invoking the subscribe(onNext:onError:onCompleted:onDisposed:) method. Every time a new value is emitted by the subject's observable, we print that value to the console.

import RxSwift
import Foundation

let subject = PublishSubject<Int>()

subject.subscribe(onNext: { (value) in
    print(value)
})

Last but not least, we emit two values by invoking the on(_:) method on the subject.

import RxSwift
import Foundation

let subject = PublishSubject<Int>()

subject.subscribe(onNext: { (value) in
    print(value)
})

subject.on(.next(3))
subject.on(.next(4))

Run the contents of the playground and inspect the output in the console. You should see the values 3 and 4 printed to the console. That's how easy it is to add RxSwift and RxCocoa to a project using CocoaPods.

3
4

What's Next?

Installing RxSwift and RxCocoa using CocoaPods is trivial. RxSwift and RxCocoa also support Carthage and the Swift Package Manager. You can find installation instructions on GitHub.