Wwdc18:402: Getting the Most out of Playgrounds in Xcode

Swift was officially released by apple on WWDC14, and Xcode6 was released at the same time that Playground was integrated for the first time. Two years later, at WWDC16, Apple announced Swift Playground, an iPad proprietary software to help young people learn to use Swift better. At WWDC18 this year, Apple talked to us again about making the most of Xcode’s Playground feature. If that doesn’t convince you to embrace Swift, the language of the future, Apple may have to cry.

Not many updates, but machine learning

There aren’t many updates to Playground in Xcode 10 released at the conference, but one major focus is on the model Playground uses to train machine learning, simply by importing the new Create ML framework. There are already big guys playing on Weibo, and it looks good.

Here’s an update on Playground in Xcode 10:

Playground tailored for machine learning

  • The repL-like pattern (read-eval-print-loop) can quickly re-run the existing playground code;
  • Run the code before the specified line, or “Shift + Enter” to run the code you just wrote;
  • Import the new Create ML machine learning framework interactively trains the new model, writing code in playground to test the model. Once complete, apply the model directly to the application.

There will be a separate session for the Create ML framework.

The basic function

For a basic introduction to playground, check out the official help documentation. Some basic uses of Playground include Live View and Markup language.

Live View

We can visually view our own views in the Playground. It’s also simple to implement:

import PlaygroundSupport
let viewController = /* Your attempt controller */

PlaygroundPage.current.liveView = viewController
Copy the code

You can use Live View to quickly test animations or special effects you want to make. This format is great for learning about official apis, or creating tutorials. Meng To, a famous foreign designer, wrote the animation Design part of the “Design+Code” tutorial with playground. The effect of each line of Code can be directly seen in the Live View, which is very intuitive.

Markup

Playground is markdown annotated rendering, requiring only a colon after a single or multi-line comment. In a sense, you can use Playground as a markdown editor. Playground also supports resources in a variety of formats, including audio, video, images, and more. Furthermore, you can refer to these resources either in markup language or in code. Such as:

// You can reference image resources in markup language! [alternate text](MyPicture.jpg "hover title")
// It can also be referenced directly in code
 let image = UIImage(named: "image.jpg")
Copy the code

Playground supports multiple pages. Create a new Playground Page. It is also easy to jump between pages in three ways:

[The previous page] (@previous)
[The next page] (@next)
[The specified page] (PageName)
Copy the code

Rendering is as simple as opening the right-hand bar and checking “Render Documentation” in the File Inspector.

For detailed markup language syntax, see the Apple documentation. Meanwhile, Apple has an official playground file template of Swift language standard library. You can visit and learn it. Maybe we can also try to write a document with playground.

Step by step (new feature in Xcode 10)

Running by step is a new feature in Xcode 10. When the mouse moves over the line of code on the Playground, the line number column turns blue. Click the play key on the line number to run the code above the current line (including the current line).

The play-key for code that has already been run turns gray, as does the play-key if the mouse line does not form a complete code block (such as in a loop).

One thing to note here is that code that has already been executed will not be run again. To run it again, click the Stop button on the Debug toolbar below. After resetting Playground, we’ll notice that the line number before the previously run code is blue again, in the “runnable” state.

In what situations is playground suggested? The development team also gave advice:

  1. Step – by – step use of our API tutorial
  2. When dealing with data, such as map data, public statistics, class assignments, etc
  3. Test a game demo or test an app animation

Advanced techniques

Custom display type

Xcode 4.1 9.3 / Swift CustomPlaygroundDisplayConvertible CustomPlaygroundQuickLookable replaced before. Can be custom CustomPlaygroundDisplayConvertible agreement return value display type in the Playground.

extension MyType: CustomPlaygroundDisplayConvertible { 
    var playgroundDescription: Any{... }}Copy the code

The following code redefines the return value of the Pitch as a view.

import xxx

let pitch = Pitch(.a, 4)

// Set the return type to view
extention Pitch : CustomPlaygroundDisplayConvertible {
    public var playgroundDescription: Any {
    // Create a view and define its properties
    let view = createView()
    
    view.noteNumber = noteNumber
    view.octave = octave
    view.customText = description
    
    // Return to view
    return view
    }
}

Copy the code

The running results are as follows:

Custom types can be all types supported by Xcode 9.3/Swift 4.1.

Use custom frameworks in Playground

It is also easy to reference custom frameworks. For a simple framework, just drag the Playground file into the project.

If you have multiple frames, put them in a workspace and drag the Playground file into it.

Reference the framework and import it directly.

Finally, the people on the development team said their vision was:

Next year, each project will have a Playground file!

Do you think their vision will come true?


In general, the use scenario of Playground is still focused on learning and trial play. After all, the interactive interface design allows learners to see the results of their own codes quickly, so feedback is very important for initial learning. We’ve also seen a lot of effort from the development team to make the things we did on Playground directly applicable to the formal project.

So, as the second speaker said, Playground is a “fun” place, but it is “serious fun” because everything in it is reusable. Xcode’s Playground is Apple’s Playground for developers, so keep playing!

PS, here are the resources related to Xcode Playground mentioned in this article:

  1. A Github Repo that collects all kinds of great playgrounds
  2. Apple’s official Xcode help document
  3. Apple Markup syntax document
  4. Playground file template for the Apple Swift language Standard Library documentation