Swift 6 cheatsheet
Swift 6: SwiftUI basics
5 common Swift 6 tasks.
A simple view
A SwiftUI view returns its content from body. State changes re-render it.
import SwiftUI
struct CounterView: View {
@State private var count = 0
var body: some View {
VStack(spacing: 12) {
Text("Count: \(count)")
Button("Add") { count += 1 }
}
.padding()
}
}
Show a list
List and ForEach build rows from identifiable data.
List(items) { item in
Text(item.name)
}
Navigate between screens
NavigationStack with NavigationLink pushes detail screens.
NavigationStack {
List(items) { item in
NavigationLink(item.name, value: item)
}
.navigationDestination(for: Item.self) { item in DetailView(item: item) }
}
Bind a control to state
A $ prefix passes a two-way binding to a control.
@State private var name = ""
TextField("Name", text: $name)
Load data when a view appears
The task modifier runs async work and cancels it when the view goes away.
.task {
items = (try? await loadItems()) ?? []
}