WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: SwiftUI in Playgrounds

Forums > SwiftUI

I'm really confused about the whole Playground workflow. I have an iPad, when I create a playground, it is SwiftUI only. But then when I create a Playground in Xcode on my Mac, it is only UIKit. Is it possible to create a SwiftUI playground via Xcode on the Mac?

   

I get a SwiftUI playground when I make one from my mac and I thought it was always like that

   

When you create a new Playground in Xcode, you can choose the template as either iOS or macOS. The option is in the top left corner.

Then you choose the type of Playground - Blank, Game, … which will set up the Playground with any necessary imports and base code.

   

I found my answer on this page

So I created an iOS SingleView playground and replaced the generated code with this:

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, World!")
            Spacer()
            Text("Goodbye, World!")
        }
    }
}

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

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.