Contents

tinder/collectionbuilders

 

Overview

Creating or modifying arrays and sets using result builders allows for inline control statements such as if/else, switch, for loops and availability checking. Additionally, commas are not used to separate collection elements, yielding cleaner syntax than array and set literals.

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.

API

Array

init(@Builder elements: () -> Self)
static func build(@Builder elements: () -> Self) -> Self
mutating func append(@Builder elements: () -> Self)
func appending(@Builder elements: () -> Self) -> Self

Set

init(@Builder elements: () -> Self)
static func build(@Builder elements: () -> Self) -> Self
mutating func insert(@Builder elements: () -> Self)
func inserting(@Builder elements: () -> Self) -> Self

Installation

Swift Package Collection

<details><summary>Expand / Collapse</summary><br>

https://swiftpackageindex.com/Tinder/collection.json

</details>

Swift Package Manager

<details><summary>Expand / Collapse</summary><br>

Package Dependency

Replace <version> with the desired minimum version.

.package(url: "https://github.com/Tinder/CollectionBuilders.git", from: "<version>")

Target Dependency

"CollectionBuilders"

</details>

Xcode

<details><summary>Expand / Collapse</summary><br>

Package Dependency

https://github.com/Tinder/CollectionBuilders.git

Target Dependency

CollectionBuilders

</details>

Supported Types

The Array and Set result builders may be used with any Element type.

Code Examples

Examples of common use cases:

Array<NSLayoutConstraint>

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)
})

Set<AnyCancellable>

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)
}

Contributing

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.

License

Licensed under the Match Group Modified 3-Clause BSD License.

Package Metadata

Repository: tinder/collectionbuilders

Default branch: main

README: README.md