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

Swift Result Builders Cheat Sheet

Swift Result Builders Cheat Sheet

Covers writing @resultBuilder types, buildBlock/buildOptional/buildEither methods, and how SwiftUI's ViewBuilder and DSLs like this work under the hood.

2 PagesAdvancedJan 22, 2026

Defining a Result Builder

A result builder is a type with static `buildBlock`-family methods, applied via `@resultBuilder`.

swift
@resultBuilderstruct HTMLBuilder {    static func buildBlock(_ components: String...) -> String {        components.joined(separator: "\n")    }}@HTMLBuilderfunc page() -> String {    "<h1>Title</h1>"    "<p>Body text</p>"}print(page())// <h1>Title</h1>// <p>Body text</p>

Supporting `if`/`else` and Optionals

Implement `buildEither` and `buildOptional` to allow control flow inside the builder closure.

swift
@resultBuilderstruct HTMLBuilder {    static func buildBlock(_ components: String...) -> String {        components.joined(separator: "\n")    }    static func buildEither(first component: String) -> String { component }    static func buildEither(second component: String) -> String { component }    static func buildOptional(_ component: String?) -> String { component ?? "" }    static func buildArray(_ components: [String]) -> String {        components.joined(separator: "\n")    }}@HTMLBuilderfunc page(isAdmin: Bool) -> String {    "<h1>Dashboard</h1>"    if isAdmin {        "<button>Admin Panel</button>"    } else {        "<p>Standard user</p>"    }    for i in 1...3 {        "<li>Item \(i)</li>"    }}

SwiftUI-style Usage

This is exactly the mechanism behind `@ViewBuilder`, `@SceneBuilder`, and `Regex` builders.

swift
struct ContentView: View {    var body: some View {        VStack {          // VStack's init takes @ViewBuilder content: () -> Content            Text("Hello")            if isLoggedIn {                Text("Welcome back")            }        }    }}// Applying a builder to a closure parameter:func container(@HTMLBuilder content: () -> String) -> String {    "<div>\(content())</div>"}

Result Builder Static Methods

The methods the compiler looks for, and when each is invoked.

  • buildBlock(_:)- combines a sequence of statements into one result (required)
  • buildOptional(_:)- handles `if` without `else` (result is optional)
  • buildEither(first:)/(second:)- handles `if/else` and `switch` branches
  • buildArray(_:)- handles `for` loops inside the builder
  • buildExpression(_:)- transforms each individual expression before blocking
  • buildLimitedAvailability(_:)- handles `if #available` type erasure
  • buildFinalResult(_:)- wraps/transforms the final built value before returning
Pro Tip

Implement buildExpression(_:) to accept multiple input types (e.g. String and Image) and normalize them to one internal representation — that's how SwiftUI lets ViewBuilder mix wildly different View types in one block.

Was this cheat sheet helpful?

Explore Topics

#SwiftResultBuilders#SwiftResultBuildersCheatSheet#Programming#Advanced#DefiningAResultBuilder#SupportingIfElseAndOptionals#SwiftUIStyleUsage#Result#Functions#CheatSheet#SkillVeris