Home > Net >  How to create a closure to use with @escaping
How to create a closure to use with @escaping

Time:03-01

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:

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]'

CodePudding user response:

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

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