Making a playground using RxSwift

Note: Updated on 3/10/2017 to use Xcode 8.2.1 and CocoaPods 1.2.0.

Situation: You’re using RxSwift in a project. You realize that it would be really nice to have a playground to test out ideas.

Problem: You can’t import RxSwift (or RxCocoa) into your playground. Apple’s instructions are opaque.

Solution: Use CocoaPods to install RxSwift. CocoaPods will create a workspace that includes RxSwift and your existing project. A workspace created within the project will be able to import RxSwift.

Detailed instructions: If you’re not already using CocoaPods, initialize a new Podfile:

$ pod init

Now you should have a Podfile in your project directory. Add the following segment in your Podfile:

# Pods for Project
pod 'RxSwift', '~> 3.2'

Then install this pod:

$ pod install

Now you should have a workspace (with .xcworkspace extension). Open this file with Xcode.

After installing your pods, make sure you open the workspace (on the right), not the project (on the left).

Now you can create your playground.

Go to the project navigator and unselect everything (⌘-click any selected items). This is important because when you add a project in the next step, you want it to be under the workspace and not the existing project.

Make sure nothing is selected.

Add a new Playground to the workspace using the “+” button in the project navigator.

Give your playground a name. I suggest putting it in the same directory as your Project.xcodeproj and Project.xcworkspace file. Keep things together.

At this point you should have a workspace with your project, playground, and pods all at the toplevel of the workspace:

Now build your project. This should also build the Rx target you selected in the previous step.

Your build should succeed!


Now go to your playground and import RxSwift.

Using your RxSwift Playground

You may notice that RxSwift behaves a little differently in a playground. Specifically, if you create an Observable.interval, it doesn’t seem to do anything. That’s because you need to tell Xcode to keep running your playground continuously, otherwise it simply runs every statement and immediately stops execution before the interval observable can send events.

This can be solved by adding this SupportCode.swift file under Playground / Sources.

At the end of your playground, add a call to playgroundTimeLimit to specify how long you want the playground to run. For example:


This will keep the playground running for 10 seconds, after which the playground will automatically stop, saving your laptop battery and limiting the output buffer size.