https://github.com/eonist/with
Manipulate an object with a closure
https://github.com/eonist/with
closure swift swift-package-manager swift5 with
Last synced: about 1 year ago
JSON representation
Manipulate an object with a closure
- Host: GitHub
- URL: https://github.com/eonist/with
- Owner: eonist
- License: mit
- Created: 2018-12-04T08:24:31.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-01-23T11:23:56.000Z (over 1 year ago)
- Last Synced: 2025-04-14T10:12:46.383Z (about 1 year ago)
- Topics: closure, swift, swift-package-manager, swift5, with
- Language: Swift
- Homepage:
- Size: 49.8 KB
- Stars: 16
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# With 💗
  
[](https://github.com/apple/swift)
[](https://github.com/eonist/With/actions/workflows/Tests.yml)
[](https://codebeat.co/projects/github-com-eonist-with-master)
### What is it
An extension that let's you manipulate an object with a closure
### Description:
With is a Swift extension that lets you manipulate an object with a closure. It provides a concise and expressive way to modify an object's properties without having to create temporary variables or write boilerplate code.
### How does it work
With works by taking an object and a closure as input. The closure takes an inout reference to the object, allowing you to modify its properties directly. The modified object is then returned by the with function.
See [Example](https://github.com/eonist/With#example)
### How can I get it
You can add With to your project using Swift Package Manager by adding the following line to your Package.swift file: `.package(url: "https://github.com/eonist/With.git", branch: `"master")`
### Example:
```swift
// Example 1:
let rectangle: CGRect = with(.init(x: 0, y: 0, width: 100, height: 100)) {
$0 = $0.offsetBy(dx: 20, dy: 20)
$0 = $0.insetBy(dx: 10, dy: 10)
}
Swift.print(rectangle) // X:30.0, y:30.0, width:80.0, height:80.0
// Example 2:
let color: UIColor = with(.init(red: 50, green: 100, blue: 0, alpha: 0.9)) { ( col:inout UIColor) -> Void in
col = col.withAlphaComponent(0.2)
}
Swift.print(color.cgColor.alpha) // 0.2
// Example 3:
var size: CGSize = .init(width: 50, height: 40)
with(size) {
$0.width = 100
$0.height = 50
}
Swift.print(size)//100, 50
// Example 4:
func createImageView() -> UIImageView {
return with(.init()){
$0.image = UIImage(named: "someGraphic")
self.addSubview($0)
}
}
createImage() // Adds image to view
```
**Example for with Using Key Paths:**
```swift
// Using with function with key paths to configure a UILabel
let label = UILabel()
.with(\.textColor, setTo: .red)
.with(\.text, setTo: "Hello World")
.with(\.textAlignment, setTo: .center)
.with(\.layer.cornerRadius, setTo: 5)
```
**Example for withMap:**
```swift
// Using withMap to configure DateFormatter and get a formatted date string
let dateString: String = withMap(DateFormatter()) {
$0.dateStyle = .medium
$0.timeStyle = .none
return $0.string(from: Date())
}
Swift.print(dateString) // Outputs: Jan 1, 2022
```
### Credit:
Thanks [https://github.com/sindresorhus](https://github.com/sindresorhus) for teaching me this JavaScript-esque super power 💪
### Todo:
- Add examples for withMap and with that uses keypath to readme