Declarative syntax
With the introduction of SwiftUI comes a more modern coding paradigm called declarative syntax. In this section, we’ll take a look at what exactly declarative syntax is and how it compares to the style of syntax we might be used to seeing already.
Getting ready
For this section, you’ll need the latest version of Xcode available from the Mac App Store.
How to do it…
- Open Xcode and select File | New | Playground, then select Blank in order to open a new playground canvas to work from.
- Once that’s open, add in the following syntax:
import PlaygroundSupport
import SwiftUI
We’ve seen the first
import
statement before, so it should be familiar. The next one is for SwiftUI—pretty self-explanatory as to why we need this. - Now, let’s create a view in SwiftUI by adding the following code:
struct MyView: View {
var body: some View {
...