Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/capnslipp/with
with(…) { … } Statement (a Swift µ-Library)
https://github.com/capnslipp/with
context operator operators scope swift swift-library utilities utility
Last synced: 3 months ago
JSON representation
with(…) { … } Statement (a Swift µ-Library)
- Host: GitHub
- URL: https://github.com/capnslipp/with
- Owner: capnslipp
- License: unlicense
- Created: 2020-03-06T15:34:41.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-11T19:20:43.000Z (5 months ago)
- Last Synced: 2024-10-13T10:06:41.917Z (4 months ago)
- Topics: context, operator, operators, scope, swift, swift-library, utilities, utility
- Language: Swift
- Homepage:
- Size: 51.8 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# With
With is a Swift micro-library that provides a `with` statement— _which is modeled after the `with` functionality found in Python, JavaScript, Visual Basic, Object Pascal, Delphi; `using` found in C#; and `also`/`let` found in Kotlin._
With provides a set of overloaded generic free functions that are useful for:
* Object or value initialization and setup in a declarative style _(think Swift UI's hierarchical style, but for anything, anywhere)_.
* Performing multiple operations on an object/value fetched via a method/property, while staying D.R.Y and without needing to create a local var _(while still avoiding invoking the method/getter repeatedly)_.
* Performing calculations with an object/value that only needs to live long enough to be configured and do some calc _(and you're only interested in a result object/value)_.With provides a free function `func with(_ subject: SubjectT, operations: (inout SubjectT) -> Void) -> SubjectT` that can be used on any object or value type to do stuff like this:
```swift
// initializes a value-type `hitTestOptions` dictionary for use with
// `SCNView`'s `hitTest(…)` with the desired options some of which have
// changed in newer OS versions (which the standard dictionary literal syntax
// can't cleanly do)
let hitTestOptions = with([SCNHitTestOption : Any]()) {
$0[.boundingBoxOnly] = true
$0[.rootNode] = _myRootNode
if #available(iOS 11.0, tvOS 11.0, macOS 10.13, *) {
$0[.searchMode] = SCNHitTestSearchMode.all.rawValue
} else {
$0[.sortResults] = true
$0[.firstFoundOnly] = false
}
}
```Or like this:
```swift
// initializes the object-type `newButton` with title, sizing, styling, etc.
// and adds the view to a superview
let newButton = with(UIButton(type: .system)) {
$0.titleLabel!.font = .systemFont(ofSize: 13)
$0.setTitle("My Button", for: .normal)
$0.autoresizingMask = [ .flexibleLeftMargin, .flexibleBottomMargin ]
$0.contentEdgeInsets = UIEdgeInsets(top: 6.5, left: 7, bottom: 6.5, right: 7)
with($0.layer) { layer in
layer.borderWidth = 1.0
layer.borderColor = UIColor.white.cgColor
layer.cornerRadius = 5
}
$0.sizeToFit()
rootViewController.view.addSubview($0)
}
```With also has an alternate `func withMap(_ subject: SubjectT, operations: (inout SubjectT) -> ReturnT) -> SubjectT` form that can return an arbitrary value from the closure (instead of the value passed in):
```swift
// initializes a `DateFormatter`, configures it, and uses it to calculate a
// `String` which is the only thing we want to hang onto
let dateString = withMap(DateFormatter()) {
$0.dateStyle = .medium
$0.timeStyle = .none
$0.locale = Locale(identifier: "en_US")
let currentDate = Date()
return $0.string(from: currentDate)
}
```