Swiftpack.co - Package - phlippieb/PHBApplying

PHBApplying

Version License Platform

What is this?

Applying is simply syntactic sugar. It is a method that applies a given closure to a receiving object; for example:

let nameLabel = UILabel.applying { $0.text = viewModel.name }

Applying facilitates remarkably clean initialisation code. Instead of

private lazy var button: UIButton = {
   let button = UIButton()
   button.addTarget(self, action: #selector(onTap), for: .touchUpInside)
   return button
}()

we can write

private lazy var button = UIButton()
   .applying { $0.addTarget(self, action: #selector(onTap), for: .touchUpInside) }

It is conceptually identical to Then, with different terminology. I chose to use applying because:

  • then is well-suited to describing asynchronous processes, and is commonly used in that context
  • apply is used in Kotlin to mean exactly what it means in this library, making it the established terminology
  • applying indicates that the method modifies and returns the receiver, in keeping with similar Swift terminology such as sorted.

Please note that I happened to have come up with the same idea as Then, independently. My original use case prompted me to call it Configurable (see my Medium article here). I developed it for my working project, and found myself missing it whenever I was on a differnet project, so I decided to publish it as a pod.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

PHBApplying is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'PHBApplying'

Author

phlippieb, phlippie.bosman@gmail.com

License

PHBApplying is available under the MIT license. See the LICENSE file for more info.

Github

link
Stars: 0

Dependencies

Used By

Total: 0