For this chapter, we shall look at this brilliant new feature called Playground. You can simply type the code you see in this chapter in a Playground, and it will run and show the results without having to compile or run the application. However, there are a few things to note before we start:
temp
as string,
and further down you used it as an int
, the compiler would throw an error complaining that the variable is being redefined.We can create a new Playground by navigating to File | New | Playground or by OPT + CMD + N:
The Playground is the Xcode editor without the toolbar, or the other elements. It has a sidebar that displays the results of the line of code. It is suggested that you turn on the assistant editor as it displays additional information that would be quite useful:
If you hover over the results in the main code editor, you see two icons, an eye and a circle. The eye allows you to view the value in a pop-up, if it is a primitive, such as Int
, String
, and Double,
it will display the value. If it is a complex class such as CGRect
, CGPoint
, UIBezierCurve
, and UIColor,
it is displayed visually, as in the following screenshot:
Playgrounds can show a lot of information that is otherwise difficult to visualize. For the simplest amazing example to illustrate this point, type the following code in a Playground to see the results:
var red = UIColor.redColor() var rect1 = CGRectMake(10, 10, 200, 50) var path = UIBezierPath(ovalInRect:rect1)
The following screenshot shows the Playground:
3.19.29.89