Skip to content

Instantly share code, notes, and snippets.

@donnywals
Created June 7, 2018 08:01
Show Gist options
  • Save donnywals/698b1c88dc368f018b7fe6856048f4b1 to your computer and use it in GitHub Desktop.
Save donnywals/698b1c88dc368f018b7fe6856048f4b1 to your computer and use it in GitHub Desktop.

Getting the most out of playgrounds in Xcode

Fundamentals (“old” stuff)

  • You can show results of lines of code inline with your code by clicking the square next to the result in the result sidebar
  • You can use UIView and even UIViewController to give a live preview.
  • Show a live preview with the following snippet:
import PlaygroundSupport

let vc = //
PlaygroundPage.current.liveView = vc
  • Markup text is used to show formatted text in a playground. You can show text, images and videos.
  • Markup comments start with: //:. Multiline looks like: /*: … */
  • To render markup in the playground click the button in the top right corner of the window and enable rendering.
  • Headings are created using markdown syntax (#, ##, etc.)
  • Other markup also works (backpacks for code, * for bold / italic, lists and more)
  • You link to other playground pages by creating links. For instance @next, @previous or PlaygroundFileName.
  • You can add multiple source files to a playground page, or to the whole playground. These extra files are compiled as modules and automatically imported by the playground. You use access control to expose code to the playground page.
  • You can also add images and videos in the resources folder. You can include images and videos in markup comments using the file name. You can use the content in code as well. For instance: UIImage(named: “MyImage”) or Bundle.main.url(forResource: "video", withExtension: ".mp4").

New in Playgrounds

  • There is a new “Play button” in playgrounds. This button and the blue bar it’s on means you can execute the marked lines. A grey play button means that you’re not ready to execute that line of code.
  • Shift + Return is a shortcut to run your playground.
  • The blue code can be executed without resetting / completely rerunning the playground.
  • You can manually reset your playground with the stop button in the borrow left.
  • The method of running playgrounds is way faster to execute since you can add lines without rerunning the whole playground. It’s also more stable because data isn’t different on every run.

Advanced techniques

  • CustomPlaygroundDisplayConvertible is used to provide a custom view of a type in a playground.
  • If you add a playground to your project, you can import the build result for the project into your playground. The works fine for simple projects.
  • If you have multiple projects that you want to use in a playground, it’s better to add the projects to a workspace and add the playground to the workspace as well. So don’t add the playground to a project.
  • In Project Settings, click the advanced button. From there you can easily navigate to your project’s build folder.
  • You must manually import your framework into the playground.
  • Always make sure to build before you run so the playground has all the frameworks available.

Some tips / takeaways

  • Create a playground as documentation for your frameworks and APIs
  • Use playgrounds to explore data by stepping through it line by line
  • With line by line execution it is now easier and faster to use playgrounds to explore thoughts and ideas
  • Playgrounds are a great way to explore frameworks because you don’t have to set up a full blown app to do so.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment