Playgrounds
Swift Playgrounds is an excellent way to test Swift code without creating an app or a complete project.
Unfortunately, Swift Playgrounds was developed for UIKit and does not completely support SwiftUI.
In Xcode, there is an option to create a Playground
containing a view, but that will create a view with ViewController
using UIKit, not SwiftUI, and that’s not ideal for our purposes.
You can, however, start with an empty Playground and create your view in code with SwiftUI and then set it as the current view by using PlaygroundPage.current.setLiveView(ContentView())
.
Your code will look like this:
import SwiftUI import PlaygroundSupport struct ContentView: View { var body: some View { Text("I love SwiftUI") } } PlaygroundPage.current.setLiveView(ContentView())
However, the support for SwiftUI in Swift Playgrounds is far from ideal, and not updated...