Building a Simple HTML Builder with @resultBuilder
📖 Scenario: Imagine you want to create HTML code using Swift in a clean and readable way. Swift's @resultBuilder attribute helps you build complex structures like HTML by writing code that looks like regular Swift code but produces HTML strings.
🎯 Goal: You will build a simple HTML builder using @resultBuilder that lets you write HTML elements in Swift code and get the final HTML string as output.
📋 What You'll Learn
Create a HTMLBuilder result builder to combine strings
Write a function buildHTML that uses @HTMLBuilder
Use the builder to create multiple HTML elements inside buildHTML
Print the final combined HTML string
💡 Why This Matters
🌍 Real World
Using <code>@resultBuilder</code> helps developers write clean and readable code for building complex data like HTML, JSON, or UI layouts.
💼 Career
Understanding <code>@resultBuilder</code> is useful for Swift developers working on SwiftUI, DSLs, or any code that benefits from declarative syntax.
Progress0 / 4 steps
1
Create the HTMLBuilder result builder
Create a struct called HTMLBuilder with the @resultBuilder attribute. Inside it, write a static function buildBlock that takes a variadic parameter of String and returns a single String by joining all parts.
Swift
Hint
Use @resultBuilder before the struct name. The buildBlock function combines all strings into one.
2
Create the buildHTML function using @HTMLBuilder
Write a function called buildHTML that uses the @HTMLBuilder attribute. The function should return a String and have no parameters. Inside the function, return multiple HTML element strings like "
Hello
" and "
Welcome to Swift
" on separate lines.
Swift
Hint
Use @HTMLBuilder before the function. Return multiple strings inside the function body.
3
Call buildHTML and store the result
Create a constant called html and assign it the result of calling buildHTML().
Swift
Hint
Use let html = buildHTML() to store the result.
4
Print the final HTML string
Write a print statement to display the value of the html constant.
Swift
Hint
Use print(html) to show the final HTML string.
Practice
(1/5)
1. What is the main purpose of the @resultBuilder attribute in Swift?
easy
A. To combine multiple expressions into a single result in a clean way
B. To declare a new type of variable
C. To handle errors automatically
D. To create a new thread for concurrent execution
Solution
Step 1: Understand the role of @resultBuilder
@resultBuilder is used to combine multiple code lines or expressions into one result, making code cleaner and easier to write.
Step 2: Compare options with the purpose
Options B, C, and D describe unrelated features. Only To combine multiple expressions into a single result in a clean way correctly describes the purpose of @resultBuilder.
Final Answer:
To combine multiple expressions into a single result in a clean way -> Option A
Quick Check:
@resultBuilder purpose = combine expressions [OK]
Hint: Remember: @resultBuilder combines code lines into one result [OK]
Common Mistakes:
Confusing @resultBuilder with error handling
Thinking it creates new variables
Assuming it manages concurrency
2. Which of the following is the correct way to declare a custom result builder in Swift?
easy
A. @resultBuilder class MyBuilder { func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } }
B. @resultBuilder struct MyBuilder { static func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } }
C. @resultBuilder func MyBuilder(_ components: Int...) -> Int { components.reduce(0, +) }
D. struct MyBuilder { func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } }
D. Function buildString should not have @MyBuilder
Solution
Step 1: Check buildBlock method declaration
In a result builder, buildBlock must be a static method. Here it is an instance method (missing static).
Step 2: Verify other parts
Return statement is present, String type is allowed, and function parameter correctly uses @MyBuilder.
Final Answer:
buildBlock must be static -> Option C
Quick Check:
buildBlock static keyword required [OK]
Hint: Always make buildBlock static in result builders [OK]
Common Mistakes:
Omitting static keyword on buildBlock
Confusing return statement presence
Thinking @resultBuilder restricts types
5. You want to create a custom result builder that builds a dictionary from key-value pairs written inside a closure. Which of the following implementations correctly uses @resultBuilder to achieve this?