In this episode of the Swift introduction series we’ll be looking at Playgrounds in Swift.
What are Playgrounds?
As well as the REPL (covered in last week’s episode) Xcode comes with a new interactive environment called the Playground. This allows developers to interactively try out content as well as seeing both the final results and intermediate calculations, leading to some impressive possibilities.
Playgrounds are created with Xcode with “File → New → Playground”.
This creates a folder on disk called
name.playground, and inside a simple
Swift file called
section-1.swift that starts out with a simple
The left side of the screen has the code, and the right hand side shows the intermediate output for each line of code being executed. The examples from the previous post can be added into the the playground to see the results.
As well as single results, there is a console which can be displayed to show
console output (as geenrated by
println) or intermediate lines:
In fact, a good debugging trick with Playgrounds is to put an intermediate value on a new line, even if it isn’t used then graph that by viewing it with the add value button:
Playgrounds can be associated with either iOS or OSX playforms (though OSX has been temporarily removed from Xcode 6.0; it should be back in Xcode 6.1)
In next week’s episode, we’ll look at how to create functions in Swift. To subscribe to this series, add the Swift tag feed to your reader.