Skip to content

Swift Result Builders for Array and Set

License

Notifications You must be signed in to change notification settings

Tinder/CollectionBuilders

Folders and files

NameName
Last commit message
Last commit date

Latest commit

784bbe3 · Jan 26, 2025

History

22 Commits
Jan 26, 2025
Jan 26, 2025
Jan 26, 2025
Jan 26, 2025
Jan 26, 2025
Jan 26, 2025
Sep 2, 2023
Jan 26, 2025
Jan 26, 2025
Jan 26, 2025
Jan 26, 2025
Jan 26, 2025
Jan 26, 2025

Repository files navigation

Swift   DocC   Artifactory

Collection Builders

Swift Result Builders for Array and Set

Overview

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.

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

Minimum Requirements

  • iOS 13.0
  • Swift 5.8

Installation

Swift Package Manager Dependency

Replace <version> with the desired minimum version.

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

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.