Guides and Sample Code


Playground Book Format Reference

Hidden Code

You can hide code that is not related to the main content of a playground page, such as calls to page setup functions, code for assessing the progress of the learner, and functions used in editable examples. Hidden code is executed when the playground is run and all of the public symbols are accessible on the playground page.

  • //#-hidden-code
  • line of code
  • //#-end-hidden-code

Hidden code is placed between the hidden-code and end-hidden-code delimiters. The opening and closing delimiters are preceded by two forward slash characters followed by a pound sign and a hyphen (//#-) and appear on a separate line.

You can place hidden code at the top and bottom of a playground page. Additionally, on pages with at least one editable area, you can place hidden code anywhere on the page outside of an editable area.

Listing 10-1 uses hidden code to show a live view. The user has access to symbols in UKit and PlaygroundSupport, even though the import statements are hidden.

Listing 10-1Hiding the code to show a live view
  1. /*:
  2.  Change properties of `theView` and run the playground.
  3.  Try modifying:
  4.  * `CGColor`: `markColor` and `backgroundColor`
  5.  * `Bool`: `isChecked`
  6.  * `CGFloat`: `strokeFactor`, `insetFactor`, and `markInsetFactor`
  7.  */
  8. //#-hidden-code
  9. import UIKit
  10. import PlaygroundSupport
  11. let viewRect = CGRect(x: 0, y: 0, width: 100 , height: 400)
  12. let theView = CustomView(frame: viewRect)
  13. PlaygroundPage.current.liveView = theView
  14. //#-end-hidden-code
  15. //#-editable-code
  16. theView.markColor = UIColor.darkGray
  17. theView.isChecked = true
  18. //#-end-editable-code

Figure 10-1 shows the playground page rendered from Listing 10-1. The code on lines 8-12, which is inside the hidden code delimiters, is not shown on the playground page. theView is used to change properties of the live view, even though it is defined in the hidden code.

Figure 10-1Playground page with hidden code image: ../Art/SP_hidden_code_2x.png