100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Views and the View Protocol

How the `View` protocol works, what `body` must return, and how SwiftUI composes small views into larger interfaces.

iOS & SwiftUI FoundationsBeginner7 min readJul 8, 2026
Analogies

Views and the View Protocol

Every visible element in SwiftUI — from a single Text label to an entire screen — conforms to the View protocol. The protocol's sole requirement is a computed property named body that returns some View, describing what should be displayed. Because body itself returns another view (which may itself be composed of further views), SwiftUI interfaces are built as trees of small, composable pieces rather than a handful of large, monolithic view classes as is common in UIKit.

🏏

Cricket analogy: Every player on the field, from a fielder to the captain, conforms to a shared role like the View protocol; each has a 'body' of responsibilities, and a whole team's strategy is built by composing individual player roles, not one monolithic plan.

Composing Views

The idiomatic SwiftUI style favors extracting small, single-purpose views rather than writing one enormous body. A ProfileHeader view might combine an Image, a Text, and a Button — and that composed view can then be reused inside a ProfileScreen, a SettingsScreen, or anywhere else it's needed. This composability is enabled by the fact that every custom view is itself just another type conforming to View, indistinguishable from SwiftUI's own built-in views like Text or Image from the framework's perspective.

🏏

Cricket analogy: Instead of one player trying to bat, bowl, and field all at once, a captain like Rohit Sharma composes a team from specialists — an opener, a spinner, a wicketkeeper — reused across matches, just as small views compose into a screen.

swift
struct ProfileHeader: View {
    let username: String
    let avatarSystemName: String

    var body: some View {
        VStack(spacing: 8) {
            Image(systemName: avatarSystemName)
                .resizable()
                .frame(width: 64, height: 64)
                .clipShape(Circle())
            Text(username)
                .font(.title2.bold())
        }
    }
}

struct ProfileScreen: View {
    var body: some View {
        VStack {
            ProfileHeader(username: "ada.codes", avatarSystemName: "person.crop.circle")
            Text("Joined 2024")
                .foregroundStyle(.secondary)
        }
        .padding()
    }
}

Why `body` Returns `some View`, Not `View`

You cannot write var body: View directly, because View has an associated-type requirement (Body) that makes it unusable as a plain return type — Swift needs to know the concrete type at compile time for performance and type-checking. some View resolves this: it tells the compiler 'there is one specific, concrete type here, infer it, but hide it from callers.' This is why every path through a body implementation (e.g. both branches of an if) must return the same concrete view type, or SwiftUI wraps them using constructs like _ConditionalContent behind the scenes via @ViewBuilder.

🏏

Cricket analogy: You can't say 'the team will field one specific undetermined formation' without naming it; 'some View' is like a captain saying 'trust me, there's one specific fielding plan, I just won't announce it publicly' before the toss.

body's implicit @ViewBuilder attribute is what allows you to list multiple views inside a VStack { ... } without explicit array syntax or return statements — it's a Swift result builder that stitches sibling views into a single composed type.

A common beginner mistake is writing conditional logic across incompatible view types (e.g. returning Text in one branch and Image in another) without realizing @ViewBuilder handles this via _ConditionalContent — this works fine in practice, but forgetting it exists can cause confusion when reading compiler-generated type errors.

  • The View protocol requires only a body property describing what to render.
  • SwiftUI interfaces are trees of small, composable custom views, not monolithic screen classes.
  • Extracting reusable subviews (like a ProfileHeader) is idiomatic SwiftUI style.
  • body returns some View, an opaque type, because View has an associated type and can't be used as a direct return type.
  • @ViewBuilder (implicitly applied to body) lets you list sibling views without explicit array or return syntax.
  • Custom views are indistinguishable from built-in views like Text from SwiftUI's perspective.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#IOSWithSwiftUIStudyNotes#MobileDevelopment#ViewsAndTheViewProtocol#Views#View#Protocol#Composing#StudyNotes#SkillVeris