Use Swift Playgrounds to sketch ideas

Before spinning up a whole Swift project, use Swift Playgrounds to sketch out ideas.

In Xcode (macOS):

  • File -> New -> Playground
  • Choose “Single View” (I’m not sure what impact picking macOS or iOS has)
  • Choose a name and location to save your playground

UIKit is the iOS default for a single-view playground. Storybooks are the default for macOS. To replace those with a SwiftUI component:

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
  var body: some View {
    VStack {
      Button("Tap me!") {
      }
    }
  }
}

let contentView = ContentView()
PlaygroundPage.current.setLiveView(contentView)

🤔 I haven’t yet come upon a good heuristic for:

  • when to spin up a project for tinkering vs. a playground
  • to what extent one could use TDD in playgrounds
  • how to import files from a playground inside a project and use project resources
Adam Keys @therealadam