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.

How do you stay current as a Swift developer?

Let me do the hard work and join 20,342 developers that stay up to date using my weekly newsletter:

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!

 
Antoine van der Lee

Written by

Antoine van der Lee

iOS Developer since 2010, former Staff iOS Engineer at WeTransfer and currently full-time Indie Developer & Founder at SwiftLee. Writing a new blog post every week related to Swift, iOS and Xcode. Regular speaker and workshop host.

Are you ready to

Turn your side projects into independence?

Learn my proven steps to transform your passion into profit.