Guides and Sample Code


Playground Book Format Reference

On This Page

PlaygroundPage Class

An object that represents the state of a playground page and enables you to interact with the Swift Playgrounds live view.


A PlaygroundPage object provides methods and properties that represent the state of a playground page, enabling you to interact with Swift Playgrounds. Functionality includes displaying a live view, accessing the text in the playground, checking the assessment status, ending the execution of a playground, and accessing the live view.

Class Properties


The current playground page.


  • static let current: PlaygroundPage


Use current to find the playground page instance.

Instance Properties


The assessment status for the page, if any; otherwise nil.


  • var assessmentStatus: AssessmentStatus? { get set }


Set the assessment status to display success text or update the hints for the page.

Assessment status for a page is initialized to nil, indicating that the page has not been assessed.

For possible values, see AssessmentStatus Enumeration.

For example, to show the success text “You did it!” set the assessment status to .pass, as shown below.

  1. PlaygroundPage.current.assessmentStatus = .pass(message: "You did it!")


The active live view, if any; otherwise nil.


  • var liveView: PlaygroundLiveViewable? { get set }


Display a live view by setting liveView to an object that conforms to the PlaygroundLiveViewable Protocol. The live view is displayed in the live view pane for the current playground page. There can be only one live view open at any time.

To open a live view, set PlaygroundPage.current.liveView to an instance of a class that inherits from UIViewController or UIView. The live view can be opened from any source file and runs only while the playground is executing. When liveView is set to a non-nil value, the system sets needsIndefiniteExecution to true. Dismiss an open live view by setting liveView to nil.

Live views opened from LiveView.swift start running when the playground is opened and do not require the playground to be executing. Setting liveView to nil does not dismiss an always-on live view. Message sending between an always-on live view and the playground page requires a proxy. For more information, see PlaygroundRemoteLiveViewProxy Class.

image: ../Art/SP_PlaygroundSupport_PlaygroundPage_liveView_2x.png


A Boolean value that indicates whether indefinite execution is enabled.


  • var needsIndefiniteExecution: Bool { get set }


By default, all top-level code is executed, and then execution stops. When working with asynchronous code, enable indefinite execution to allow execution to continue after the end of the playground’s top-level code is reached. This, in turn, gives threads and callbacks time to execute.

A user editing the playground or touching the Stop button stops execution automatically, even when indefinite execution is enabled.

Set needsIndefiniteExecution to true to continue execution after top-level code stops executing. Set it to false before the end of the top-level code is reached to stop execution after top-level code stops. Setting it to false while the playground is executing continuously has no effect. To stop execution at any time, use finishExecution().

The default value is false. It is set to true when liveView is set to a non-nil value.


The current contents of the playground page, including any user-entered text.


  • var text: String { get }


The following code checks if the text of the playground page contains one of the strings “llama” or “Llama”. If it does, a success dialog is presented.

  1. let pageText = PlaygroundPage.current.text
  2. if pageText.contains("llama") || pageText.contains("Llama") {
  3.    PlaygroundPage.current.assessmentStatus = .pass(message: "That's the right animal")
  4. }

Instance Methods


Stops executing the current playground page.


  • func finishExecution() -> Never