Key Takeaways
View Protocol is the central protocol of SwiftUI. It requires a single property: var body: some View. body is a computed property that returns the UI description. SwiftUI calls body on every state change and renders the result on the GPU.
@ViewBuilder is a Result Builder for Views. It allows writing multiple Views inside a closure without return and without TupleView. It supports if/else, switch, ForEach. All containers (VStack, HStack, ZStack, Group) use @ViewBuilder. An alternative is Group { … } for grouping without layout. some View is an Opaque Type. The compiler knows the concrete View type (Text, Button, VStack, etc.), but returns it externally as some View. This allows changing the body type without changing the signature. ViewModifier is a View modifier: .font(), .padding(), .background(). Custom modifier via ViewModifier protocol and .modifier().
// Example View with @ViewBuilder and NavigationStack
struct ContentView: View {
@State private var items = ["Item 1", "Item 2"]
var body: some View {
NavigationStack {
List(items, id: .self) { item in
NavigationLink(item) {
DetailView(item: item)
}
}
.navigationTitle("Items")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Add") {
items.append("New Item")
}
}
}
}
}
}
// DetailView with @StateObject
struct DetailView: View {
let item: String
@StateObject private var viewModel = DetailViewModel()
var body: some View {
Text(item)
.task {
await viewModel.loadDetails(for: item)
}
}
}NavigationStack (iOS 16+) — data-driven navigation. Takes a path (array of hashable values) and displays destination via navigationDestination. Replaces NavigationLink with destination views. NavigationLink — button for navigating the stack. List — vertical list with sections, swipes, deletion, and drag-and-drop support. Form — a specialized List for settings: sections, pickers, toggles, text fields. Section — grouping in List/Form with header and footer. ViewModifier — custom modifiers for reusing styles. .modifier(CustomModifier()) — application.
List in SwiftUI is the equivalent of UITableView. Supports .onDelete, .onMove, .refreshable. SwipeActions — .swipeActions { Button("Delete", role: .destructive) { } }. Form — optimized for settings: DatePicker, Toggle, Stepper, Picker. Section with header/footer — for grouping. OutlineGroup — hierarchical lists (equivalent of UITableView with sections). DisclosureGroup — collapsible section with content. IT Sectr recommends List for data and Form for settings.
PreviewProvider — protocol for previews in Xcode Canvas. Canvas — interactive SwiftUI preview (requires @main and Swift 5.9+). @ScaledMetric — adaptive size that scales with Dynamic Type.
Property Wrappers are the foundation of state management in SwiftUI. @State — local state (value type). @Binding — connection to parent state. @StateObject — creates an ObservableObject. SwiftUI manages the object's lifecycle. @ObservedObject — observes an external ObservableObject. @EnvironmentObject — object from the environment (dependency injection via .environmentObject()). @Environment — system values (.colorScheme, .locale). @AppStorage — UserDefaults. @FetchRequest — Core Data query. @FocusState — input focus.
| Wrapper | Purpose | Ownership |
|---|---|---|
| @State | Local state (value type) | View owns |
| @Binding | Connection to parent state | Parent owns |
| @StateObject | Creates ObservableObject | View owns |
| @ObservedObject | Observes external object | External owner |
| @EnvironmentObject | Object from environment | Ancestor owns |
| @Environment | System value | System |
| @AppStorage | UserDefaults | UserDefaults |
| @FocusState | Input focus | View owns |
@StateObject vs @ObservedObject: @StateObject creates an object, @ObservedObject observes it. Use @StateObject for initial model creation in the hierarchy, @ObservedObject for passing to child views. @FocusState — focus management: .focused($field, equals: .email). Supports enum for multiple fields.
@Environment — system values: colorScheme, locale, sizeCategory, layoutDirection, managedObjectContext. Custom Environment Values — custom value via EnvironmentKey. Used for dependency injection: passing services without explicit parameters. .environment() modifier — sets a value in the hierarchy. @EnvironmentObject — ObservableObject from .environmentObject(). Differs from @Environment in that it passes a reference type. IT Sectr recommends @Environment for system values and @EnvironmentObject for services.
.task{} — async operation on view appearance. Auto-cancellation on disappearance. Replaces .onAppear for async/await. .onAppear{} — synchronous operation on appearance. .onDisappear{} — operation on disappearance. .onChange(of:) — reaction to value changes. .onReceive(Publisher) — reaction to Combine publisher. View lifecycle in SwiftUI: init → onAppear → body (rendering) → onDisappear. On state change: body → diff → re-render. IT Sectr recommends .task for async loading and .onAppear for analytics.
.animation() — animation modifier. .transition() — appear/disappear animation. withAnimation — explicit state change animation. matchedGeometryEffect — animation between different views (hero animation). PhaseAnimator (iOS 17+) — multi-phase animation based on enum. KeyframeAnimator (iOS 17+) — frame-by-frame animation with keyframes. SwiftUI animations run on the GPU via Core Animation. IT Sectr recommends .spring() for natural animations and .interpolatingSpring for physics effects.
ObservableObject — protocol for objects with @Published properties. SwiftUI subscribes to the objectWillChange publisher. @Published — publishes property changes. @StateObject — creates ObservableObject, manages lifecycle. @ObservedObject — observes external. Combine — framework for reactive programming. @EnvironmentObject — DI via environment. .environmentObject() sets, @EnvironmentObject reads. MVVM — standard architecture: View (SwiftUI) — ViewModel (ObservableObject) — Model. IT Sectr recommends MVVM + @StateObject for main ViewModels.
class UserViewModel: ObservableObject {
@Published var users: [User] = []
@Published var isLoading = false
func loadUsers() async {
isLoading = true
defer { isLoading = false }
do {
users = try await api.fetchUsers()
} catch {
print("Error: \(error)")
}
}
}
struct UsersView: View {
@StateObject private var viewModel = UserViewModel()
var body: some View {
List(viewModel.users, id: \.id) { user in
Text(user.name)
}
.task { await viewModel.loadUsers() }
.overlay {
if viewModel.isLoading { ProgressView() }
}
}
}Accessibility in SwiftUI: .accessibilityLabel(), .accessibilityHint(), .accessibilityValue(), .accessibilityAddTraits(). VoiceOver reads accessibility labels. Dynamic Type — @ScaledMetric, @DynamicProperty. Localization: String(localized:) (SwiftUI + String Catalog). LocalizedStringKey — automatic translation. Plural rules — via String Catalog (one, few, many, other). Right-to-Left (Arabic, Hebrew) — supported automatically. .environment(.layoutDirection, .rightToLeft). IT Sectr recommends accessibility labels for all interactive elements.
struct AccessibleButton: View {
let action: () -> Void
var body: some View {
Button(action: action) {
Label("Settings", systemImage: "gear")
}
.accessibilityLabel("Open Settings")
.accessibilityHint("Double tap to navigate to settings screen")
.accessibilityAddTraits(.isButton)
}
}
// Dynamic Type support
struct ScalableText: View {
@ScaledMetric private var fontSize: CGFloat = 17
var body: some View {
Text("Hello, World!")
.font(.system(size: fontSize))
}
}Swift Charts (iOS 16+) — framework for building charts. Chart { } — basic container. BarMark, LineMark, PointMark, AreaMark, RuleMark — marks. ForEach for data. ChartAxis — axis customization. .chartXAxis, .chartYAxis. ChartLegend — legend. Interpolation — catmullRom, linear, step. Animation — .animation() for Chart. IT Sectr recommends Swift Charts for built-in charts and Charts (DGCharts) for complex ones.
import Charts
struct MonthlySales: Identifiable {
let id = UUID()
let month: String
let sales: Double
}
struct SalesChart: View {
let data: [MonthlySales]
var body: some View {
Chart(data) { item in
BarMark(
x: .value("Month", item.month),
y: .value("Sales", item.sales)
)
.foregroundStyle(.blue.gradient)
}
.chartXAxisLabel("Month")
.chartYAxisLabel("Sales")
.frame(height: 200)
.padding()
}
}Swift Package Manager (SPM) — Apple's dependency manager. Integrated into Xcode. Supports: binary packages, resources, tests, swift-6 strict concurrency. Package publishing via GitHub with semver tag. SPM is the de facto standard for Swift dependencies, replacing CocoaPods and Carthage. IT Sectr recommends SPM for all new projects.
Swift Concurrency (async/await, TaskGroup, AsyncSequence) — async/await built into Swift 5.5+. Task — unit of asynchrony. TaskGroup — parallel tasks. AsyncSequence — async sequence (AsyncStream, AsyncAlgorithms). Task Local Values — contextual data for Task. Swift Concurrency fully replaces Combine for async operations.
Frequently Asked Questions
View Protocol is the base protocol. Requires var body: some View. body is the UI description, rendered on the GPU.
@ViewBuilder is a Result Builder for multiple Views. Allows if/else, switch, ForEach inside containers.
@StateObject creates an object. @ObservedObject observes an existing one. @StateObject for first creation in the hierarchy.
NavigationStack — data-driven navigation. path-based, deep linking. Replaces NavigationView.
@FocusState — input focus management. .focused() modifier. Supports enum for multiple fields.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.