tezvyn:

SwiftUI's View: A Blueprint, Not a Building

AI-drafted, machine-checkedSource: developer.apple.combeginner
SwiftUI's View: A Blueprint, Not a Building

A SwiftUI View is a lightweight blueprint for a piece of your UI, not the UI element itself. It's used to compose everything from a single button to an entire screen. The common mistake is treating it like a heavy `UIView` class; it's a cheap.

WHY IT EXISTS: To provide a declarative way to define user interfaces. In older frameworks, you manually created, positioned, and updated UI components (imperative style). With SwiftUI, you simply declare what the UI should look like for a given state, and the framework figures out how to display it and update it efficiently. This dramatically simplifies UI code and reduces a whole class of bugs related to state synchronization.

THE MENTAL MODEL: A View is a recipe for a piece of UI. It's a lightweight struct that describes what you want to see, based on some data. It is not the actual pixels on the screen, nor is it a persistent object like a UIView from UIKit. SwiftUI takes these recipes, compares them to what's already on screen, and efficiently calculates the minimum changes needed to update the display. Because they are so cheap to create and destroy, SwiftUI can regenerate them constantly without a performance penalty.

HOW IT WORKS: Every type that conforms to the View protocol must implement a single computed property: body. The body property itself returns something that is also a View. This creates a tree-like structure. For example, a MyProfileView might have a body containing a VStack, which in turn contains an Image and a Text. When a View's state changes (e.g., a user taps a button that modifies an @State variable), SwiftUI re-computes the body of that View and its children, diffs the new view hierarchy against the old one, and applies only the necessary changes to the screen.

WHEN TO USE IT: You use the View protocol for every single UI element in a SwiftUI application. From a simple Text or Image to a complex custom screen composed of many smaller views, everything conforms to View. It is the fundamental building block of all SwiftUI interfaces.

WHEN NOT TO USE IT: Do not use the View protocol for non-UI logic. Business logic, data fetching, and complex state management should live outside your views, typically in objects that conform to ObservableObject. Trying to put this logic directly inside a View struct is a common mistake that leads to tangled code and unexpected behavior, as views are frequently destroyed and recreated. A View describes what to show, not how to fetch or compute the data it needs.

ONE CANONICAL EXAMPLE: A simple View that displays a piece of text. You define a struct, like 'GreetingView', that conforms to 'View'. It has a property for the data it needs, like 'let name: String'. Its required 'body' property returns another View, like 'Text("Hello, \(name)!")'. This 'GreetingView' is a blueprint. You create an instance like 'GreetingView(name: "World")', and SwiftUI uses its 'body' recipe to render a Text view on screen.

Read the original → developer.apple.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.