---
title: "Playgrounds bites - Little Bites of Cocoa"
description: "Little Bites of Cocoa about Playgrounds: short tips and techniques for iOS and Mac development, each a brief explanation of one concept, technique or tool."
url: "https://littlebitesofcocoa.com/topics/60-playgrounds"
author: "Jake Marsh"
---
# Bites about: [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)

Topics

- [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)

## [#139: Visualizing Results in Xcode Playgrounds 🎪📈](https://littlebitesofcocoa.com/139-visualizing-results-in-xcode-playgrounds)

 Thursday December 3rd, 2015

Topics

- [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)

One of the most useful parts of *Xcode Playgrounds* is the way they allow us to visualize our code. We can click the **Quick Look** button, or insert a preview *directly into our code* with the **Show Result** button in the **Results sidebar**:

![Image 1 of 2 from bite #139: Visualizing Results in Xcode Playgrounds](https://media.littlebitesofcocoa.com/bite-assets/139-visualizing-results-in-xcode-playgrounds-results-sidebar.png)

We can add visual representations to the **assistant timeline** using *XCPlayground's* captureValue function ([Bite #136](https://littlebitesofcocoa.com/136-xcplayground-basics)).

This can be incredibly handy for spotting problems quickly:

![Image 2 of 2 from bite #139: Visualizing Results in Xcode Playgrounds](https://media.littlebitesofcocoa.com/bite-assets/139-visualizing-results-in-xcode-playgrounds-example.png)

Tons of system types are supported like **CGPath**, **NSURLs**, and even many of **SpriteKit's** classes. These are actually the same representations that can be seen if we **Quick Look** something while debugging in **Xcode**.

We can add these capabilities to our own custom **types** (for *both*debugging and*Playgrounds*) by adding a **function** like this to them, returning a supported type:

```swift
func debugQuickLookObject() -> AnyObject? { return name }
```

This is great, but only works on types that inherit from **NSObject** and won't work for things like **Swift structs**. Also, even though we're adding this to our own type, we'll still need to return one of the built-in, Quick Look-able types supported by **Xcode**.

For more flexibility, let's look at *XCPlayground's* **XCPlaygroundLiveViewable** protocol.

This will let us visualize our own **types** by returning a **view** or **view controller** that will be shown in the **assistant timeline** when we capture a value:

```swift
extension Person : XCPlaygroundLiveViewable {
  func playgroundLiveViewRepresentation() -> XCPlaygroundLiveViewRepresentation {
    let vc = PersonLiveViewableViewController(person: self)
    return .ViewController(vc)
  }
}
```

Now, we can set our **current page's** `liveView` to a instance of Person *directly*, and we'll see our custom **view controller** appear in the **assistant timeline**. Neat!

```swift
XCPlaygroundPage.currentPage.liveView = Person()
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
```

Topics

- [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)
- [Libraries](https://littlebitesofcocoa.com/topics/7-libraries)

## [#138: Using CocoaPods in Xcode Playgrounds 🍩🎪](https://littlebitesofcocoa.com/138-using-cocoapods-in-xcode-playgrounds)

 Wednesday December 2nd, 2015

Topics

- [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)
- [Libraries](https://littlebitesofcocoa.com/topics/7-libraries)

We're continuing our look at *Xcode Playgrounds* today with *CocoaPods*. Let's see what it takes to import a *CocoaPod* into a *Playground*.

We'll begin by creating a new **Xcode Project**. Next, we'll head into our **project's** directory and run `pod init` to generate our `Podfile`. We'll open it up and configure it just as we usually would:

```ruby
platform :ios, '9.0'
use_frameworks!
link_with 'Spaceships'

pod 'Alamofire'
```

Then we'll run `pod install` to generate the **Workspace** file for our project and install our pods. We're almost done, next we need create a new *Playground* and add it to our project.

Finally, we need to add our *Playground* to our **Podfile's** `link_with` directive (*only its name, not the .playground extension*):

```ruby
link_with 'Spaceships', 'scratch-pad'
```

We'll run `pod install` one more time and we're done. We can now import the pod we installed into our *Playground* and try it out. Neat!

![Image from bite #138: Using CocoaPods in Xcode Playgrounds ](https://media.littlebitesofcocoa.com/bite-assets/138-using-cocoapods-in-xcode-playgrounds-example.png)

Topics

- [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)

## [#137: Rich Comments in Xcode Playgrounds 🎪💭](https://littlebitesofcocoa.com/137-rich-comments-in-xcode-playgrounds)

 Tuesday December 1st, 2015

Topics

- [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)

*Xcode Playgrounds* are great for trying out code quickly, or testing out a new API or framework. They can also be a wonderful form of interactive documentation.

Today we'll look at a few ways we can write our own interactive docs using *rich comments* in *Xcode Playgrounds*.

One of the primary ways we'll turn our *Playgrounds* into documentation is through *rich comments*.

*Playgrounds* have two "markup" modes: raw vs. rendered. As their names suggest, each mode specifies whether **Xcode** should "render" *rich comments* in our *Playground*, or make them editable.

We'll begin by creating a new *Playground*. Then we'll select **Editor** > **Show Rendered Markup** to switch modes.

Right away, we can see the top comment changes from:

```swift
//: Playground - noun: a place where people can play
```

To this:

![Image from bite #137: Rich Comments in Xcode Playgrounds](https://media.littlebitesofcocoa.com/bite-assets/137-rich-comments-in-xcode-playgrounds-example.png)

We can write our own *rich comments* by adding a `:` symbol after the `//` in any comment to convert it to a rich one. Neat!

*Rich comments'* Markup syntax is *heavily* inspired by [Markdown](https://daringfireball.net/projects/markdown/). (It's basically just [Markdown](https://daringfireball.net/projects/markdown/)).

[Apple has a syntax guide](https://developer.apple.com/library/ios/documentation/Xcode/Reference/xcode_markup_formatting_ref/GeneralMarkupSyntax.html#//apple_ref/doc/uid/TP40016497-CH52-SW1), but here's the idea:

```swift
//: We can write regular text, **bold** text or *italic* text.
//: ## Headers
```

We can include links in our *rich comments*:

```swift
//: A great [link](http://littlebitesofcocoa.com).
```

We can allow readers to navigate pages with some special links:

```swift
//: [Next](@next) or [Previous](@previous)
```

Or link to specific pages (names will need to be URL encoded):

```swift
//: [View Models](View%20Models)
```

We can even include images in our rich comments. We just need to first add them to our *Playground's* **Resources** **group**, then use **Markdown's** normal image syntax:

```swift
//: ![](playgrounds-rock.gif)
```

Topics

- [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)

## [#136: XCPlayground Basics 🎪](https://littlebitesofcocoa.com/136-xcplayground-basics)

 Monday November 30th, 2015

Topics

- [Playgrounds](https://littlebitesofcocoa.com/topics/60-playgrounds)

*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 *value* 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)!

 

 
