Learn Swift by Building Applications
上QQ阅读APP看书,第一时间看更新

What is a playground?

Straight from the file generated by Xcode:

"Playground – noun: a place where people can play."

Playground is a simple project, which can be used to practice, experiment, prototype and play with code and the underlying OS. It's easy to write some code to sketch an idea or to clarify a concept. The best thing is that you can mix text (markup) with code. Also, it's possible to use the native functions that are specific for the platform (for example iOS). This makes playground projects very expressive and realistic.

Let's create a playground and pe deeper into it:

  1. Open Xcode
  2. Select File New | Playground or simply press "Option + cmd + Shift + N"
  3. Pick a nice name—for example Swift 4 by examples
  4. Set the platform to iOS
  5. Click Next
  6. Select a place to save the file
  7. Press Create

As a result of these steps, you will have a new playground, which will only have a few rows.

The playground can contain more than one page.

In our example, we are using just a single-page playground, but if you want to express something complex, you can add another page using the + button in the Navigator panel (at the very bottom).