[swift-users] Can't make an escaping closure argument optional?

Kenny Leung kenny_leung at pobox.com
Fri Jan 5 13:47:55 CST 2018


Hi All.

It seems that if you have an escaping closure argument, you can’t make it optional. Am I right?
    init (
        owner:AnyObject,
        handler:@escaping (HXObserverNotification)->Void
        ) {
        self.owner = owner
        self.handler = handler
    }

You could try this:
    init (
        owner:AnyObject,
        handler:@escaping ((HXObserverNotification)->Void)?
        ) {
        self.owner = owner
        self.handler = handler
    }
You get “@escaping attribute only applies to function types”

Or you could try this:
    init (
        owner:AnyObject,
        handler:(@escaping (HXObserverNotification)->Void)?
        ) {
        self.owner = owner
        self.handler = handler
    }
You get “@escaping attribute may only be used in function parameter position”

-Kenny


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20180105/721de4aa/attachment.html>


More information about the swift-users mailing list