Welcome to StringBuilder, a Swift package for dynamically building strings
using a resultBuilder
.
To build a basic multi-line string:
String {
"Line 1"
"Line 2"
}
Becomes:
"""
Line 1
Line 2
"""
A custom separator can be used to control how the strings are joined:
String(separator: " != ") {
"String 1"
"String 2"
}
Becomes:
"String 1 != String 2"
Any type that conforms to CustomStringConvertible
can be added to the
string builder:
String {
1234
true
UUID()
}
Becomes:
"""
1234
true
3D5E76E1-CB1D-4FEF-A5CA-3C67AA08BF47
"""
String builders can be nested with different separators at each level:
String {
"One"
String(separator: ".") {
"Two"
"Three"
}
"Four"
String {
String {
"Five"
String(separator: "\n\n") {
"Six"
"Seven"
}
"Eight"
}
"Nine"
}
"Ten"
}
Becomes:
"""
One
Two.Three
Four
Five
Six
Seven
Eight
Nine
Ten
"""
StringBuilder is distributed using the Swift Package Manager. To install it within another Swift package, add it as a dependency within your Package.swift
manifest:
let package = Package(
// . . .
dependencies: [
.package(url: "https://github.com/mattcox/StringBuilder.git", branch: "main")
],
// . . .
)
If you’d like to use StringBuilder within an iOS, macOS, watchOS or tvOS app, then use Xcode’s File > Add Packages...
menu command to add it to your project.
Import StringBuilder
wherever you’d like to use it:
import StringBuilder