Implementing SwiftUI views using Playground
One of the most surprising and useful innovations brought by Swift is the Playground, a simple file that contains Swift code and that can be seen and modified in real time without waiting for a full cycle of a rebuild.
Again, Playground supports UIKit, but since SwiftUI can be quickly embedded in a UIKit component, we can use Playground for rapidly prototyping SwiftUI views too.
Getting ready
Create a Playground file (File | New | Playground) in Xcode called SwiftUIPlayground
.
How to do it
The power of Playground is that you can make a change and immediately see the result. Importing the PlaygroundSupport
framework, you can also render a UIViewController
class to be shown during the Playground session.
We are going to define a simple SwiftUI view to be rendered in Playground:
- Let's start by importing the needed frameworks:
import PlaygroundSupport import SwiftUI
- Then we create an extension to the
Text
component...