Skip to content

GSM-MSG/Configure

Repository files navigation

Configure

Extended methods for fluent syntax in Swift

Document

Contents

Requirements

  • Swift 5.0+
  • iOS 11.0+
  • tvOS 11.0+
  • macOS 10.13+
  • watchOS 4.0+

Overview

Extended methods for fluent syntax in Swift

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Installation

Swift Package Manager

Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

To integrate Configure into your Xcode project using Swift Package Manager, add it to the dependencies value of your Package.swift:

dependencies: [
    .package(url: "https://github.com/GSM-MSG/Configure.git", .upToNextMajor(from: "1.0.0"))
]

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate MSGLayout into your project manually.

Functionality

  • .set Sets the value of the property identified by the given key path.
  • .then Executes the given closure with the object as its argument.
  • .mutate Mutates the object with the given closure.
  • .let Applies the given closure to the object and returns the result.
  • .do Executes the given closure with the object as its argument.

Usage

set(_:_:)

Sets the value of a property using a key path.

let label = UILabel()
    .set(\.text, "Hello, world!")
    .set(\.textColor, .red)

then(_:)

Executes a closure with the object as its argument.

let label = UILabel().then {
    $0.text = "Hello, world!"
    $0.textColor = .red
}

mutate(_:)

Mutates the object with the given closure.

view.frame.mutate {
    $0.origin.x = 100
    $0.size.width = 150
}

let(_:)

Applies the given closure to the object and returns the result.

let dateString = Date().let {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    return formatter.string(from: $0)
}

do(_:)

Executes the given closure with the object as its argument.

UserDefaults.standard.do {
    $0.set(42, forKey: "number")
    $0.set("hello", forKey: "string")
    $0.set(true, forKey: "bool")
}