---
title: "#136: XCPlayground Basics 🎪"
url: https://littlebitesofcocoa.com/136-xcplayground-basics
author: Jake Marsh
date: 2015-11-30
published: 2015-11-30
topics: ["Playgrounds"]
description: "Xcode Playgrounds arrived in Xcode 6. They're a great way to try out some code quickly, or to learn about an API or library through interactive documentation."
---

# #136: XCPlayground Basics 🎪

By Jake Marsh, November 30, 2015. HTML: https://littlebitesofcocoa.com/136-xcplayground-basics

_Xcode Playgrounds_ arrived in **Xcode 6**. They're a great way to try out some code quickly, or to learn about an API or library through interactive documentation.

_Playgrounds_ are unbelievably useful out-of-the-box, but today we'll look at a framework called _XCPlayground_ that makes them even better.

_XCPlayground_ ships with Xcode. We can import it in our _Playgrounds_ to enable all sorts of interesting functionality.

We'll begin by creating a new _Playground_. We can do this by selecting **File** > **New** > **Playground…** from **Xcode's** menu (or **⌥⇧⌘N**). Then we'll import _XCPlayground_:

``` swift
import XCPlayground
```

Let's start with one of the most common reasons for importing _XCPlayground_, indefinite execution. By default _Playgrounds_ execute the code in each page, from top-to-bottom, then they stop executing. We can change this behavior like so:

``` swift
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
```

This will allow any asynchronous code we have in our _Playground_ to continue running indefinitely.

Once our code is done, we can call:

``` swift
XCPlaygroundPage.currentPage.finishExecution()
```

Another great use for _Playgrounds_ is prototyping **views**. We'll need to enable `needsIndefiniteExecution` first, then:

``` swift
XCPlaygroundPage.currentPage.liveView = containerView
```

We set the **current page's** `liveView` **property** to any **UIView** or **NSView** and it will appear visually in the **assistant editor**.

If the **assistant editor** isn't already visible, we can open it by selecting **View** > **Assistant Editor** > **Show Assistant Editor** from **Xcode's** menu (or **⌥⌘↩︎**).

Similarly, we can add a <i>value</i> to the **assistant editor** (including a corresponding label so know what's what) like this:

``` swift
XCPlaygroundPage.currentPage.captureValue(containerWidth, withIdentifier: "Width")
```

We'll be looking at more neat features of _Playgrounds_ soon. Please send your favorite _Playground_ tips to [hello@littlebitesofcocoa.com](mailto:hello@littlebitesofcocoa.com)!
