Search code examples
swift-playgroundswiftuixcode11

Using SwiftUI in Xcode 11 playground


I know how to use SwiftUI in Xcode 11 in a regular app project, but I was wondering if there was a way to use it in playgrounds as well, even if I couldn't use the live editor?


Solution

  • Sure, it's pretty easy:

    import PlaygroundSupport
    
    PlaygroundPage.current.liveView = UIHostingController(rootView: PlaygroundRootView())
    
    public struct PlaygroundRootView: View {
        public init() {}
    
        public var body: some View {
            Text("Hello World!")
        }
    }
    

    see more here: https://github.com/attoPascal/SwiftUI-Tutorial-Playground