- 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 evenUIViewController
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
orPlaygroundFileName
. - 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”)
orBundle.main.url(forResource: "video", withExtension: ".mp4")
.
- 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.
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.
- 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.