Swift Result Builders for Array
and Set
Creating or modifying arrays and sets using result builders allows for inline logic such as if/else
, switch
, for
loops and availability checking. Also commas are not needed to separate elements.
When used with Combine for example, the result builder may be used to add AnyCancellable
instances to a Set
without calling store(in:)
on each instance individually.
See sample code (below) for examples of common use cases.
init(@Builder elements: () -> Self)
static func build(@Builder elements: () -> Self) -> Self
mutating func append(@Builder elements: () -> Self)
func appending(@Builder elements: () -> Self) -> Self
init(@Builder elements: () -> Self)
static func build(@Builder elements: () -> Self) -> Self
mutating func insert(@Builder elements: () -> Self)
func inserting(@Builder elements: () -> Self) -> Self
- iOS
13.0
- Swift
5.8
Package Dependency
Replace
<version>
with the desired minimum version.
.package(url: "https://github.com/Tinder/CollectionBuilders.git", from: "<version>")
Target Dependency
"CollectionBuilders"
https://swiftpackageindex.com/Tinder/collection.json
The Array
and Set
result builders may be used with any Element
type.
Examples of common use cases:
NSLayoutConstraint.activate(Array {
subview.leftAnchor.constraint(equalTo: view.leftAnchor)
subview.rightAnchor.constraint(equalTo: view.rightAnchor)
subview.topAnchor.constraint(equalTo: view.topAnchor)
subview.bottomAnchor.constraint(equalTo: view.bottomAnchor)
})
var cancellables = Set<AnyCancellable>()
cancellables.formUnion(Set {
publisherA.sink { value in }
publisherB.assign(to: \.keyPath, on: object)
})
OR
var cancellables = Set<AnyCancellable>()
cancellables.insert {
publisherA.sink { value in }
publisherB.assign(to: \.keyPath, on: object)
}
While interest in contributing to this project is appreciated, it has been open sourced solely for the purpose of sharing with the community. This means we are unable to accept outside contributions at this time and pull requests will not be reviewed or merged. To report a security concern or vulnerability, please submit a GitHub issue.
Licensed under the Match Group Modified 3-Clause BSD License.