You ever notice that you're building super long guard
statements, or have tons of guards in a row?
guard let id = player.id, let name = player.name, let status = player.status else {
throw "Player might be missing fields id, name, and/or status"
}
Instead of that, just require
those fields:
let (id, name, status) = try require(from: player, \.id, \.name, \.status)
The require
function creates those guards for you ๐
You can pass up to 10 KeyPaths.
- Add the Swift package
or
- Copy Require.swift to your project.
The source is generated with GYB (brew install nshipster/formulae/gyb
), and formatted with SwiftFormat:
gyb Sources/Require/Require.swift.gyb -o Sources/Require/Require.swift --line-directive '' &&
mint run nicklockwood/SwiftFormat Sources/Require/Require.swift \
--wrapparameters after-first \
--wrapparameters after-first \
--wrapcollections after-first \
--enable spaceInsideGenerics,spaceAroundGenerics,spaceInsideParens,spaceAroundParens,consecutiveSpaces,consecutiveBlankLines