I have the following function
static func promptToHandleAutoLink(onEdit: () -> ()) {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Edit", style: .default) { _ in
onEdit()
})
...
}
I am getting the compilation error
Escaping closure captures non-escaping parameter 'onEdit'
When I look at the defination of UIAlertAction
constructor, it looks like
@available(iOS 8.0, *)
@MainActor open class UIAlertAction : NSObject, NSCopying {
public convenience init(title: String?, style: UIAlertAction.Style, handler: ((UIAlertAction) -> Void)? = nil)
}
As, there is no @escaping
keyword for handler
, I thought handler
suppose to be a non-escaping closure.
If that is so, why I am getting such compiler error?
I know I can solve this issue by simply adding @escaping
keyword on onEdit
.
static func promptToHandleAutoLink(onEdit: @escaping () -> ()) {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Edit", style: .default) { _ in
onEdit()
})
...
}
But still, I would like to understand, the reason I am getting such compiler error, because UIAlertAction
's handler
seems like a non-escaping closure.
Thanks.