🧱 Result Builders in Swift — Explained Using SwiftUI and UIViewKit
Before SwiftUI, building user interfaces in UIKit often meant writing long, linear setup code like this:
let label = UILabel()
label.text = “Hello”
let button = UIButton()
button.setTitle(”World”, for: .normal)
stackView.addArrangedSubview(label)
stackView.addArrangedSubview(button)Or alternatively, using Interface Builder, dragging and nesting views visually.
When SwiftUI arrived in 2019, Apple wanted developers to define UI hierarchies directly in code, but in a declarative and readable way.
That’s how the concept behind @resultBuilder was born — a compiler feature that turns nested Swift code blocks into structured values.
It’s the hidden mechanism that lets you write:
VStack {
Text(”Hello”)
Text(”World”)
}…and have it compiled into a tree of views.
Two years later, in April 2021, Xcode 12.5 shipped with Swift 5.4, which included the renamed, refined, and public @resultBuilder (previously private and called @_functionBuilder).
At that point, developers could create their own DSLs (Domain-Specific Languages) that benefit from @resultBuilder.
Examples include Regex, HTML, and UIKit/UIView layout DSLs.
🧩 Is @resultBuilder an Apple Invention?
Yes and no. The idea of defining tree structures through pyramidal code already existed in other ecosystems:
Year
Language / Framework
Concept
1991 HTML — Hierarchical markup structure
2007 C# LINQ expressions — Declarative query syntax
2010 AngularJS — Declarative HTML templates
2011 Scala Macros / Builders — Compile-time DSL expansion
2013 React & JSX — Declarative component trees
2016 Kotlin Lambdas with Receiver — Language-level DSL support
2016 Anko (JetBrains, Android) — Kotlin DSL for Android View hierarchies
2019 Swift @_functionBuilder used by SwiftUI’s @ViewBuilder
🧠 Does Kotlin Have an @resultBuilder-like Construct?
Not exactly — but the same effect can be achieved in Kotlin using: Lambdas with receiver, and @DslMarker
Together, they provide similar declarative syntax and scope safety, allowing Kotlin developers to build UI trees and DSLs such as Anko and Jetpack Compose.
🧱 UIViewKit Framework — @resultBuilders for UIKit
The UIViewKit framework (published in September 2023) implements @resultBuilder to create subview hierarchies, define constraints, and configure views.
It provides two main result builders: ibSubviews and ibAttributes.
Let’s see both of them in action:
import UIViewKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.ibSubviews {
UILabel().ibAttributes {
$0.centerXAnchor.constraint(equalTo: view.centerXAnchor)
$0.centerYAnchor.constraint(equalTo: view.centerYAnchor)
$0.text = “Hello, world!”
}
}
}
}ibSubviews adds its views as subviews of the parent view.
ibAttributes allows the definition of constraints and configuration of the view.
The syntax is similar to SwiftUI, but influenced by Interface Builder and the use of @IBOutlet conventions.
⚙️ Could This Be Done Without Result Builders?
Technically, yes — but the syntax would require more array brackets, commas.
Handling if, else, or for-in logic would become verbose, and adding preprocessor or macro support would be far more complex.
@resultBuilder makes such code expressive, type-safe, and concise.
🚀 Try UIViewKit
If you write UIKit code, try the UIViewKit framework.
https://github.com/Adobels/UIViewKit
Check out the ibSubviews and ibAttributes @resultBuilder implementations to see how they work — and how they make UIKit code feel modern, structured, and fun to write.


