Swift “guard” Capture Specifier Pitch
guardcaptures behave likeweakcaptures (e.g.guardcaptures 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
unownedcaptures ofselfenable implicitselfcalls within the body of escaping closures. This is not straightforward to support forweakclosures 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