Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a closure to use with @escaping

I have this function signature on a delegate

func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:])

I need to create a closure on a Singleton to forward that delegate method, then I do this:

typealias sessionDidReceiveUserInfoHandler = (WCSession, [String : Any]) -> Void
var sessionDidReceiveUserInfo: sessionDidReceiveUserInfoHandler?

and I use it like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let mySingleton = MySingleton.sharedInstance
mySingleton.sessionDidReceiveUserInfo = {(session, userInfo) in }

Now I have this function signature with the @escaping clause

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {

How do I do that for that signature. Xcode will not let me to add the escaping to the typealias like this:

typealias sessionDidReceiveMessageReplyHandlerHandler = (WCSession, [String : Any], @escaping ([String : Any])) -> Void

and if I create a typealias without the escaping it will not let me use inside the function with the error message

Cannot convert value of type ‘([String : Any]) -> Void’ to expected
argument type ‘[String : Any]’

>Solution :

You’re missing a return type on the closure (ie inside the last )):

typealias sessionDidReceiveMessageReplyHandlerHandler = (WCSession, [String : Any], @escaping ([String : Any]) -> Void) -> Void
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading