randombits
randombits

Reputation: 48490

How do you implement a closure that's an argument in a Swift 3 method?

An example method signature from WKWebView:

func evaluateJavaScript(_ javaScriptString: String, 
      completionHandler: ((Any?, Error?) -> Void)? = nil)

How does one correctly implement this method to be able to capture the Any? and Error? arguments in the completionHandler closure? I want to be able to use both of them in a print statement for instance. I can't seem to get the syntax correct for the implementation, though.

Upvotes: 0

Views: 42

Answers (1)

i_am_jorf
i_am_jorf

Reputation: 54640

evaluateJavaScript(javaScriptString: yourString, completionHandler: { result, error in 
   if let error = error {
      print("error: \(error)")
   }

   if let result = result {
      print("result: \(result)")
   }

   // Your code here
})

Upvotes: 3

Related Questions