Give your simulator superpowers

RocketSim: An Essential Developer Tool
as recommended by Apple

#Playground Macro: Running Code Snippets in Xcode’s canvas

Xcode 26 introduced a new #Playground macro that allows you to run code snippets and preview them in Xcode’s canvas. It’s a great way to quickly experiment with code inside your projects without having to define an individual .playground file.

Having this all integrated inside Xcode feels great and will make you use playgrounds way more often. The #Playground macro can be used anywhere in your Swift files, which makes it a great tool to add to your workflow. Let’s dive in!

Running code snippets using the #Playground macro

Imagine having the following Person struct:

struct Person {
    let firstName: String
    let lastName: String
    
    var fullName: String {
        firstName + " " + lastName
    }
}

You might want to quickly experiment and validate the fullName property to see how it looks. Usually, you might run your app and print out an example in the debugger:

let person = Person(firstName: "Antoine", lastName: "van der Lee")
print(person.fullName)

However, it’s more convenient to have this visible right away using the #Playground macro. To do this, you’ll have to import the Playgrounds framework and open the Canvas if needed:

import Playgrounds

#Playground {
    let person = Person(firstName: "Antoine", lastName: "van der Lee")
    print(person.fullName)
}

This eventually results in Xcode showing the results inside the canvas:

An example of the #Playground macro running a code snippet inside Xcode 26.
An example of the #Playground macro running a code snippet inside Xcode 26.
FREE 5-day email course: The Swift Concurrency Playbook by Antoine van der Lee

FREE 5-Day Email Course: The Swift Concurrency Playbook

A FREE 5-day email course revealing the 5 biggest mistakes iOS developers make with with async/await that lead to App Store rejections And migration projects taking months instead of days (even if you've been writing Swift for years)

Configuring a name for your Playground macro

You might end up using this feature a lot, even adding multiple playgrounds inside the same Swift file. In these cases, it might help to add an identifying name to one of the #Playground macros:

#Playground("Initials first name", body: {
    let person = Person(firstName: "A.J.", lastName: "van der Lee")
    print(person.fullName)
})

Navigating between multiple #Playground’s

Adding multiple playgrounds is well supported. Just like with SwiftUI previews, you’ll be able to navigate between them using the tabs at the top:

You can use the tabs at the top to navigate between multiple #Playground macros.
You can use the tabs at the top to navigate between multiple #Playground macros.

Conclusion

The new #Playground macro is a great new tool to quickly experiment with code from your project. There’s no need to create a separate playground file anymore. You only need to import the Playgrounds framework and you’re ready to go.

If you want to learn more about Macros, I encourage you to read Swift Macros: Extend Swift with New Kinds of Expressions.

Thanks!