Swift: Playgrounds

Some fun playground facts:

iOS and OS X playgrounds are different things. You cannot create an OS X playground and then “fix” it by importing UIKit. Make sure you build the playground that matches your project. This is what the XML for an iOS playground looks like. It’s part of the .xcplayground file wrapper, which you can open by right-clicking a playground in Finder and showing package contents. Specifically, notice the sdk in the playground key.

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<playground version=’1.0′ sdk=’iphonesimulator’>
<sections>
<code source-file-name=’section-1.swift’/>
</sections>
</playground>

And this is the XML for a Cocoa playground:

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<playground version=’3.0′ sdk=’macosx’>
<sections>
<code source-file-name=’section-1.swift’/>
</sections>
<timeline fileName=’timeline.xctimeline’/>
</playground>

Accessing items outside the playground. Lots of people want to import classes from their current project classes and test them in the playground.  I tried playing with the sections by adding extra code files, but all that did was add them directly to my playground scrollback. Wordy and not very helpful. I also tried changing standalone to “no” but as far as I can tell, the playground is its own little world and I don’t yet see a way to import anything other than modules.

The best I’ve been able to do is copy/paste functions and such into the playground for tweaking, testing, and use by other playground items. It’s wordy and not really what I was looking for. If you’ve figured out a workaround, please share!

What’s in that playground? From what I can tell, a standard playbox bundle consists of the contents file, a swift source file, and either an xml timeline or Results.playgrounddata binary file. Be very careful about modifying these with Xcode open.

Console vs Results I’ve noticed a few people get confused by this (hi Jonathan!), trying to track down the “missing” output of their println and NSLog output. These appear in the assistant window, in the Console Output. To access them, click the Assistant Editor button or choose View > Assistant Editor > Show Assistant Editor

QuickLook and Value Histories. When you hover your mouse in the results section (not the assistant, but the one that automatically appears to the right of the playground input), you’ll find a quicklook icon (looks like an eye) and a value history dot (looks like a dot). Tap the eye to visually preview items you’ve created. When you tap on a Value History dot, it opens a new inspector in the assistant. This inspector enables you to track an item’s value history over time, so you can see how (for example) a variable changes through each iteration of a for loop. One of my favorite instances of this involved a line like var xb = center.x + r * sin(theta + dTheta). The value history for this was a splendid graph!

The Timeline. The timeline mentioned in the recent talks is located at the bottom of the assistant editor. It provides a way to move back and forth through the execution of your playground, again really terrific when you’re working with loops and such. There are tiny – and + buttons built into this slider, which you use to adjust the duration of the tracked material

Reset. There’s an option for Editor > Reset Playground, which I haven’t been able to make work yet

What did I miss? Please share your Playground insights!

3 Comments

  • Thanks for the article. Do you know if there is a way to handle mouse clicks on a view in playground? I have tried many options but I can’t get anything to fire.

    • From what I understand after some of the talks touch isn’t supported in Playgrounds.

  • Is there any way to ‘pause’ the playground while you make some edits? It seems super crashy and temperamental, deleting the wrong end of a bracket first, crash, too many function calls, crash, typo, crash… etc. all day long!