Swift “guard” Capture Specifier Pitch
guard
captures behave likeweak
captures (e.g.guard
captures do not retain the captured value), but the closure body is only executed if the captured objects still exist.[…]
As of SE-0269, strong and
unowned
captures ofself
enable implicitself
calls within the body of escaping closures. This is not straightforward to support forweak
closures in the general case, and was intentionally excluded from SE-0269.[…]
guard let value = value else { return }
is quite a bit of boilerplate in this context.
Previously:
- Fixing Swift’s “if let” Syntax
- Dealing With Weak in Closure-based Delegation
- Stop the weak-strong Swift Dance