Learn how to create a new playground in Xcode. Learn how to use playground support to make a live view.
- [Instructor] Coding playgrounds in Xcode is a lot like coding any other application with a few exception. Let's explore Xcode playgrounds with a new project. I'd like to set up some code to display a menu board of my pizzas. Let's make a class for displaying pizzas using a playground feature called live view. Along the way, you learned how to work with live views, how to code playgrounds in Xcode, and how to work with source and resource files. First, go to Xcode, and close the current project. To make a new playground in Xcode, go to File, New, Playground.
Xcode will ask for a name, and the operating system. Name the file MenuBoard. You can select between iOS, macOS, and tvOS for your playground. We'll be sticking with iOS for these lessons. Xcode will ask you where to save the playground. I'd suggest saving it in the iCloud Playgrounds folder. At the top of the file, you'll find a comment. Change that, to this.
//:# Menu Board. //: A menu to print to the live view. Remove the line "Hello, playground". There's a library you'll need for working with playgrounds. The playground support framework sets up live views.
Underneath import UIKit, add import PlaygroundSupport. Playground support can work with both views, and view controllers. I'll use a view controller in this project. Add a class MenuBoard for subclass UIViewController like this. class MenuBoard:UIViewController. Once you have the class, add the viewDidLoad.
And add the super for it. And now, we'll add a background color of blue. view.backgroundColor = .blue. That will be our view controller for now. Make a few more lines under the class. Under the view controller, make an instance of the view controller. let menuBoard = MenuBoard.
There is a special class PlaygroundPage in the playground support framework. This class has a property current, that points to the current playground. Current has a property liveView, which is the current live view. When you set a view controller on the current live view, you get a view controller in the live view. Live views need to run continuously. You need to set a switch for that in playground pages.
Add this. PlaygroundPage.current.needsIndefiniteExecution = true. If you look over on the right margin, you'll see that there are some things called quick looks. So the class is running, but nothing really seems to be happening on the screen. In Xcode, the live view is in the Assistant Editor window. Click the rings for the Assistant Editor, and you'll get a blue background for the class.
Change blue to yellow, and you'll get a yellow background. We've set up a live view. Live views work with UI views, UI view controllers, SpriteKit and SceneKit using a UI view. To set up a live view, you added the content to the current playground page's live view, and then set needsIndefiniteExecution to true
- Comparing iPad and Xcode playgrounds
- Creating and testing Swift classes and code in iPad and Xcode playgrounds
- Prototyping code
- Debugging and testing with Quick Look live views
- Using markup to interactively document code and create educational lessons
- Compiling code into playground books