简体   繁体   中英

Cannot convert value of type '(_) -> ()?' to expected argument type '@convention(block) () -> Void'

This piece of code ran fine in Swift 3, but now after conversion of code by the Xcode into Swift 4, it is throwing error. I am presenting the next scene in the main queue, based on the values I got from the web-service.

DispatchQueue.main.async {
    [weak self] value in
    self?.router?.invokeTheSegueAfterTheWebService(navigationKey: arrayElementsGot["questionType"] as! String, givenViewController: self!)
}

In Swift 3, the value parameter was inferred to have the type () .

Swift 4 no longer allows you to provide a single parameter to blocks that contextually should take a different number of parameters. So your code should look like

DispatchQueue.main.async {
    [weak self] in
    self?.router?.invokeTheSegueAfterTheWebService(navigationKey: arrayElementsGot["questionType"] as! String, givenViewController: self!)
}

If you look at the error you got, it says that it expected () -> Void (eg a block with no parameters) but you passed it (_) -> () (eg a block with one parameter of unknown type).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM