A Swift Playground is a great way to prototype functions and classes for iOS and Mac development. Recently I needed a script to read out parts of a wordpress export file and fill the contents into a JSON file. The problems with Swift scripts and Xcode is, that I did not manage to get autocompletion. The autocompletion seems to be bound to projects.

I could have created a project for the development of the classes for the script. But in Xcode 6 there are playgrounds. Instant feedback for the win!

As I needed to read in an xml file I needed a way to open the file in a playground.

First I needed to find the share folder for the playgrounds.

import XCPlayground

let directoryPath = XCPSharedDataDirectoryPath

In my case the folder was at the location

/var/folders/md/7lcqg1rx07d0h3q9yp91hhdr0000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.ChangesStructureOfPhyPlist-62D0E009-7FDB-4D1A-A42F-F15CE5BBDDB9/Documents/Shared Playground Data

This is actually a symbolic link. It points to

/Users/dom/Documents/Shared Playground Data

In my case there wasn't a folder at this location so I created one. I moved the wordpress.xml file to into the shared folder. To open it in the playground I then only had to type

let path = XCPSharedDataDirectoryPath.stringByAppendingPathComponent("wordpressSmall.xml")

let inputData = NSData(contentsOfFile: path)

And the fun could begin.

Comments? Ping me on Twitter and App.net.