Xcode 6 New Feature :Playground

Swift is the new programming language introduced by Apple Inc to develop iOS and mac applications, To speed up the development process apple introduced a new feature for the developers in xcode 6, Playgrounds make coding fun and interactive, by using playgrounds you can see the output of each line immediately after typing your code, Playgrounds make testing easy and fun,write your algorithm and see the result of each line,when you make changes in your code the result you can see on the sidebar.

Adding Playground to your project?

  • Open your project.
  • Go to File—>New–>Playground.
  • Enter your playground name.
  • This will add your playground file to your project.

Figure 1.Adding playground to your project

Using Playgrounds

Here is two simple Examples that explains how to use your playground.

Example 1: Sum of 10 numbers

var sum = 0
for i in 0...10 {
    sum += i
}

Example 2: Creating view

var view=UIView()
view.frame=CGRectMake(0, 50, 100, 100)
view.backgroundColor=UIColor.purpleColor()

Figure 2:Sample playground

  • Here you can see the result of the each line on the side bar.
  • On the click of ā€œQuick Viewā€ you can see the output of a particular line.
  • On the click of ā€œValue Historyā€ you can see the timeline which contains total output of your code.
  • If you are having for loop in you code the timeline will display for loop in graphical format.
  • By clicking of particular iteration you can see the value at that index.

Limitations of Playgrounds:

  • You cannot execute your playground on iphone device
  • You cannot implement performance testing using playground