Just occurred to me today that UIKit methods like present()
or dismiss()
on view controllers now accept non-escaping closures. Is this a bug in documentation/SDK?
open func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
open func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
See: dismiss(animated:completion:) | Apple Developer Documentation
jlukas
(Jacob Lukas)
June 16, 2023, 2:19pm
2
Optional closures are always escaping.
3 Likes
tera
June 16, 2023, 4:24pm
4
Which is kind of a bug.... Could be worked around by either a longer:
open func present(_ vc: UIViewController, animated: Bool, completion: @escaping () -> Void) {
...
}
open func present(_ vc: UIViewController, animated: Bool) {
present(vc, animated: animated) {}
}
open func dismiss(animated: Bool, completion: @escaping () -> Void) {
...
}
open func dismiss(animated: Bool, completion: @escaping () -> Void) {
dismiss(animated: animated) {}
}
or a shorter:
open func present(_ vc: UIViewController, animated: Bool, completion: @escaping () -> Void = {}) {
...
}
open func dismiss(animated: Bool, completion: @escaping () -> Void = {}) {
...
}
jlukas
(Jacob Lukas)
June 16, 2023, 4:25pm
5
Sure, but in this case they should be escaping.
tera
June 16, 2023, 4:28pm
6
Ah, yep, you are right. I meant situations when closures are turned from nonescaping to escaping by virtue of being optional.
jlukas
(Jacob Lukas)
June 16, 2023, 4:38pm
7
Yes, your workarounds work when you want the closure to be nonescaping. Hopefully someday we will have nonescaping for all argument types.