Swift Playgrounds: Learn Swift Without a Full Project


When Remy from Ratatouille wanted to learn how to cook, he didn’t start by running a five-star restaurant — he experimented with ingredients in a safe little kitchen first. Swift Playgrounds give you that same kind of space: a lightweight scratch pad where you can try out code, see results instantly, and learn without the overhead of building an entire app.

In this post, you’ll learn how to create and use Playgrounds in Xcode, inspect values with the live results sidebar and print(), import frameworks, run code step by step, and explore the new #Playground macro in Xcode 26. We won’t cover building full apps — that comes later in the learning path.

What You’ll Learn

What Is a Playground?

A Playground is a special type of file in Xcode that lets you write Swift code and see the results immediately — without creating a project, setting up a simulator, or pressing a “Build and Run” button for an entire app.

Think of it like Woody’s sketch pad in Toy Story. Before Andy’s toys go on a big adventure, they can sketch out the plan on paper first. A Playground is your sketch pad for code: quick, disposable, and perfect for trying ideas.

Apple Docs: Xcode Playgrounds — Xcode

Creating Your First Playground

After you’ve installed Xcode (covered in Post 41), creating a Playground takes just a few clicks:

  1. Open Xcode.
  2. From the menu bar, choose File > New > Playground… (or press Option + Command + Shift + N).
  3. Select the Blank template under the iOS tab.
  4. Give it a name — something like “SwiftExperiments” — and click Create.

Xcode opens a new file with some starter code already in it:

import UIKit

var greeting = "Hello, playground"
"Hello, playground"

That single line of code creates a variable called greeting and stores the text "Hello, playground" inside it. You can see the result on the right side of the screen right away — that’s the live results sidebar.

The Live Results Sidebar

The live results sidebar is the narrow column on the right edge of the Playground editor. Every time a line of code produces a value, Xcode displays it there automatically.

Try changing the greeting:

var greeting = "To infinity and beyond!"
"To infinity and beyond!"

The sidebar updates to show the new value. This instant feedback loop is what makes Playgrounds so powerful for learning — you type code, and you see what it does without any extra steps.

Tip: If the sidebar feels too narrow, you can drag its left edge to make it wider, or click the eye icon next to a result to see a larger preview.

Using print() to Inspect Values

The sidebar is great for simple values, but sometimes you want more control over what you see. The print() function writes text to the console — a dedicated output area at the bottom of the Playground.

let movie = "Finding Nemo"
let year = 2003
print("My favorite movie is \(movie)!")
print("It came out in \(year).")
My favorite movie is Finding Nemo!
It came out in 2003.

The \( ) syntax is called string interpolation — it lets you insert a value directly inside a piece of text. You’ll use print() constantly when learning Swift because it gives you a clear, readable way to check what your code is doing.

Tip: If you don’t see the console, click the square icon in the bottom-left corner of the Playground editor, or choose View > Debug Area > Show Debug Area from the menu bar.

Importing Frameworks

A framework is a collection of pre-built code from Apple that gives you extra capabilities. When you created your Playground, Xcode included import UIKit at the top. That line makes all of UIKit’s tools available to you.

You can import different frameworks depending on what you want to experiment with:

import Foundation

let today = Date()
print("Today's date is \(today)")
Today's date is 2026-03-13 17:30:00 +0000

Foundation is Apple’s fundamental framework — it provides types for dates, text, numbers, and much more. For SwiftUI experiments, you would use import SwiftUI instead.

Note: The exact date and time in the output will reflect when you run the code, so your result will differ from the example above.

Running Code Step by Step

By default, a Playground runs all your code at once. But sometimes you want to run it line by line to understand what happens at each step — like watching a Pixar movie frame by frame to appreciate the animation.

Xcode gives you two ways to control execution:

  1. Run up to a line. Hover your mouse over the left gutter (the column with line numbers). A blue play button appears. Click it to run all code from the top down to that line and stop there.
  2. Run the whole Playground. Click the blue play button at the bottom-left of the editor, or press Command + Shift + Return.

This step-by-step approach is especially helpful when you’re experimenting with loops or conditions and want to see how values change over time:

var countdown = 3
print("Launch in \(countdown)...")
countdown = 2
print("Launch in \(countdown)...")
countdown = 1
print("Launch in \(countdown)...")
print("Liftoff! To infinity and beyond!")
Launch in 3...
Launch in 2...
Launch in 1...
Liftoff! To infinity and beyond!

By running up to each print() line one at a time, you can watch countdown decrease in the sidebar before each message appears.

The #Playground Macro in Xcode 26

Starting with Xcode 26, Apple introduced a powerful new way to experiment with code: the #Playground macro. Instead of creating a separate Playground file, you can add a #Playground block directly inside any Swift file in your project.

#Playground {
    let character = "Buzz Lightyear"
    let catchphrase = "To infinity and beyond!"
    print("\(character) says: \(catchphrase)")
}
Buzz Lightyear says: To infinity and beyond!

The code inside #Playground { } runs inline, right where you write it, and shows results in a live preview — without affecting the rest of your project. When you build your app for release, the compiler strips out all #Playground blocks automatically, so they never ship to users.

This is perfect for when you’re deep inside a project and want to quickly test how a piece of logic works without switching to a separate Playground file.

Note: The #Playground macro requires Xcode 26 or later. If you’re using an earlier version of Xcode, use traditional Playground files instead.

Swift Playgrounds on iPad

If you don’t have a Mac handy, you can still learn Swift using the Swift Playgrounds app on iPad. It’s a free app from Apple that provides an interactive environment for writing and running Swift code, complete with guided lessons and challenges.

The iPad app is a great companion for learning on the go — think of it as Lightning McQueen’s portable training simulator from Cars. You can practice anywhere, then bring your skills back to the full Xcode racetrack on your Mac.

You can download Swift Playgrounds from the App Store for free.

Tip: Projects you create in Swift Playgrounds on iPad can be opened in Xcode on your Mac, so your work transfers seamlessly between devices.

Common Mistakes

Forgetting to Run the Playground

Unlike a full Xcode project, Playgrounds don’t always run automatically. If you don’t see any results in the sidebar or console, click the blue play button at the bottom-left of the editor to execute your code.

Not Seeing the Console Output

If your print() statements don’t seem to produce any output, the console might be hidden. Open it by choosing View > Debug Area > Show Debug Area or clicking the bottom-left panel icon.

Writing Code That Takes Too Long

Playgrounds have a time limit for execution. If you accidentally write an infinite loop, the Playground will stop and show a warning. Always make sure your loops have a clear exit condition:

// ❌ This runs forever — the Playground will stop it
var count = 1
while true {
    count += 1
}
// ✅ This has a clear stopping point
var count = 1
while count <= 5 {
    print("Count is \(count)")
    count += 1
}
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5

What’s Next?

  • A Playground is a lightweight file for experimenting with Swift code without creating a full project.
  • The live results sidebar shows values automatically as each line runs.
  • Use print() and the console to inspect output in a clear, readable format.
  • Import frameworks like Foundation or SwiftUI to access Apple’s built-in tools.
  • Use step-by-step execution to understand how your code runs line by line.
  • The #Playground macro in Xcode 26 lets you experiment inline, right inside your project files.
  • Swift Playgrounds on iPad lets you practice Swift anywhere.

Now that you have your experimentation environment ready, it’s time to start writing real Swift code. Head over to Variables and Constants in Swift to learn about var, let, and how Swift stores data — and try every example in a Playground as you go!