📖 Scenario: Imagine you want to create a simple way to build a list of messages using a special syntax in Swift. This will help you write cleaner code when combining multiple messages.
🎯 Goal: You will create a custom result builder called MessageBuilder that combines multiple String messages into one array. Then, you will use it to build a list of messages easily.
📋 What You'll Learn
Create a custom result builder named MessageBuilder
Implement the required static function buildBlock that takes multiple String parameters and returns an array of String
Create a function buildMessages that uses the @MessageBuilder attribute
Use the buildMessages function to combine messages
Print the combined messages array
💡 Why This Matters
🌍 Real World
Custom result builders help write clean and readable code when combining multiple pieces of data, such as building UI views, SQL queries, or configuration lists.
💼 Career
Understanding result builders is useful for Swift developers working on SwiftUI, DSLs, or any code that benefits from declarative syntax.
Progress0 / 4 steps
1
Create the MessageBuilder result builder
Create a custom result builder called MessageBuilder using @resultBuilder. Inside it, write a static function buildBlock that takes a variable number of String parameters named components and returns an array of String containing those components.
Swift
Hint
Use @resultBuilder before the struct declaration. The buildBlock function should accept String... and return [String].
2
Create the buildMessages function using @MessageBuilder
Create a function called buildMessages that uses the @MessageBuilder attribute. This function should return an array of String. Inside the function, write multiple String messages on separate lines: "Hello", "Welcome", and "Goodbye".
Swift
Hint
Use @MessageBuilder before the function. Inside the function, just write the strings on separate lines without commas.
3
Call buildMessages and store the result
Call the function buildMessages() and store its result in a variable called messages.
Swift
Hint
Simply assign the call buildMessages() to messages.
4
Print the combined messages array
Print the variable messages to display the combined array of messages.
Swift
Hint
Use print(messages) to show the array.
Practice
(1/5)
1. What is the main purpose of declaring a @resultBuilder in Swift?
easy
A. To handle errors automatically in functions
B. To create a new data type like a class or struct
C. To optimize memory usage during runtime
D. To define how multiple code parts combine into a single result
Solution
Step 1: Understand the role of @resultBuilder
A @resultBuilder is used to customize how multiple code blocks combine into one result, making code cleaner.
Step 2: Compare with other options
Options A, B, and D describe unrelated concepts like error handling, data types, and optimization, which are not the purpose of result builders.
Final Answer:
To define how multiple code parts combine into a single result -> Option D
Quick Check:
Result builder = combine code parts [OK]
Hint: Result builders combine code parts into one result [OK]
Common Mistakes:
Confusing result builders with classes or structs
Thinking result builders handle errors
Assuming result builders optimize memory
2. Which of the following is the correct way to start declaring a custom result builder in Swift?
B. @resultBuilder class MyBuilder { func buildBlock() {} }
C. class MyBuilder { func buildBlock() -> Int { 0 } }
D. struct MyBuilder { static func buildBlock() {} }
Solution
Step 1: Identify correct syntax for result builder
A custom result builder must be declared with @resultBuilder attribute on a struct, and have static methods like buildBlock returning a value.
Step 2: Check each option
@resultBuilder struct MyBuilder { static func buildBlock() -> String { "" } } correctly uses @resultBuilder, declares a struct, and has a static buildBlock method returning a String. The other options miss the attribute, use class instead of struct, or have non-static buildBlock.
Step 1: Check method requirements in result builder
Methods like buildBlock inside a @resultBuilder must be declared as static so they can be called without an instance.
Step 2: Analyze the given code
The buildBlock method is missing the static keyword, which causes a compile error.
Final Answer:
buildBlock must be static -> Option A
Quick Check:
buildBlock method = static [OK]
Hint: buildBlock must always be static in result builders [OK]
Common Mistakes:
Omitting static keyword on buildBlock
Thinking buildBlock must return Int always
Believing struct can't be used with @resultBuilder
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 buildBlock implementations correctly combines multiple (String, Int) tuples into a single dictionary?