Skip to main content
InterviewHubPRO
Section Overview
Q2

Question 2 of 50

Part 1: SwiftUI Architecture, State & App Lifecycle (1 - 10)

4%
iOS Platform & SwiftUI (50 Qs)•Part 1: SwiftUI Architecture, State & App Lifecycle (1 - 10)

What is the SwiftUI View Protocol and why are views structs?

senior LevelCode ExampleLifecycleSystem Design

In Simple Words: In SwiftUI, every view conforms to the View protocol, requiring a body computed property:

  • Views are lightweight structs, not reference classes!
  • Creating, destroying, and recreating struct instances on the stack takes nanoseconds and consumes minimal memory.
  • SwiftUI continually recreates view structs to determine UI diffs, updating only the underlying render tree.
swift
import SwiftUI

struct GreetingView: View {
    let name: String

    var body: some View {
        VStack(spacing: 8) {
            Image(systemName: "hand.wave.fill")
                .foregroundColor(.blue)
            Text("Hello, \(name)!")
                .font(.headline)
        }
    }
}