Swift·Swift from optionals to SwiftUI

SwiftUI basics

Build a screen with views, state and modifiers.

Beginner 14 min read Lesson 8 of 300 Challenge included
What you will be able to do
  • Compose views with stacks.
  • Use @State for local state.
  • Pass @Binding to child views.
  • Use modifiers for layout and style.

In plain English

SwiftUI is declarative: views describe the UI for a state. @State owns local state, @Binding shares it and @StateObject owns a reference model. Views are structs and cheap to recreate.

CounterView.swiftSwift
struct CounterView: View {
    @State private var count = 0

    var body: some View {
        VStack(spacing: 12) {
            Text("Clicked \(count) times")
            Button("Increment") { count += 1 }
        }
        .padding()
    }
}
Your turn

Add padding

Easy

Add 16 points of padding to a VStack.

SWIFT · starter
VStack { }
Check your understanding before moving on.
Knowledge checkQuestion 1

Which property wrapper owns local value state?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Testing
Lesson 8 of 300 · 0% of course