Playgrounds overview

At first glance, a playground may look like a rather fancy text editor. Well, here’s some news for you: It is essentially just that!

The above screenshot highlights the first and most important things to know about:

  1. Source editor: This is the area in which you’ll write your Swift code. It’s much like a text editor such as Notepad or TextEdit. You’ll notice the use of what’s known as a monospace font, meaning all characters are the same width. This makes the code much easier to read and format.
  2. Results sidebar: This area shows the results of your code. You’ll learn more about how code is executed as you read through the book. The results sidebar will be the main place you’ll look to confirm your code is working as expected.
  3. Execution control: Playgrounds execute automatically by default, meaning you can write code and immediately see the output. This control allows you to execute the playground again. Holding down the button allows you to switch between automatic execution and manual execution modes.
  4. Activity viewer: This shows the status of the playground. In the screenshot, it shows that the playground has finished executing and is ready to handle more code in the source editor. When the playground is executing, this viewer will indicate this with a spinner.
  5. Panel controls: These toggle switches show and hide three panels, one that appears on the left, one on the bottom and one on the right. The panels each display extra information that you may need to access from time to time. You’ll usually keep them hidden, as they are in the screenshot. You’ll learn more about each of these panels as you move through the book.

Playgrounds execute the code in the source editor from top to bottom. Every time you change the code, the playground will re-execute everything. You can also force a re-execution by clicking Editor\Execute Playground. Alternatively, you can use the execution control.

You can turn on line numbers on the left side of the source editor by clicking Xcode

\Preferences...\Text Editing\Line Numbers. Line numbers can be very useful when you want to refer to parts of your code.

Once the playground execution is finished, Xcode updates the results sidebar to show the results of the corresponding line in the source editor. You’ll see how to interpret the results of your code as you work through the examples in this book.

results matching ""

    No results matching ""