Section 1: First steps with Swift and SwiftUI
Welcome to SwiftUI
SwiftUI is a declarative UI framework. Instead of writing step by step instructions such as "create a label, set its text, add it to the screen, update it when the value changes", you describe what the screen should show for the current data. When the data changes, SwiftUI works out what needs to update and does it for you.
Here is a complete SwiftUI view:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(spacing: 12) {
Image(systemName: "figure.hiking")
.font(.largeTitle)
Text("Trails")
.font(.title)
.bold()
}
.padding()
}
}
A view is a struct with a body property that returns other views. VStack stacks its children vertically. The lines starting with a dot, such as .font(.title), are modifiers. Each modifier wraps the view in a new view with that change applied, which is why their order matters.
What we are building
Trails is a hiking journal. You will be able to:
- Add a hike with a name, date, distance, difficulty and notes
- Attach a photo from your library
- Browse and search hikes in a list
- See totals for the year
- Edit and delete entries
The app stores everything locally with SwiftData, so it works without a network or an account.
Why SwiftUI now
Apple has used SwiftUI for most new frameworks and features for several years, and new APIs often arrive in SwiftUI first. UIKit is still important and you will meet it in existing apps, but for a new app SwiftUI is the natural starting point. You can mix the two when needed.
How to follow along
Each lesson has a finished project in the course repository. I recommend typing the code yourself and using the preview canvas on the right side of Xcode to see results. If your preview stops updating, the shortcut Option+Command+P resumes it.
In the next lesson we install Xcode and create the project.