0

I recently migrated from swift3 to swift 4.2. When building I now get the following error:

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

and the error appears by the following code line:

topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })

I am not really sure how to solve that error. Any help rendered will be greatly appreciated

    let topWindow = UIWindow(frame: UIScreen.main.bounds)

    topWindow.rootViewController = UIViewController()
    topWindow.windowLevel = UIWindow.Level.alert + 1
    //let storyboard = UIStoryboard(name: "Main", bundle: nil)
   // let topViewController = storyboard.instantiateViewController(withIdentifier: identifier) as? BaseViewController
    let alert = UIAlertController(title: "iPanel", message: t_alert, preferredStyle: .alert)
    let yesButton = UIAlertAction(title: get_error(eng_error: "open"), style: .default, handler: {(_ action: UIAlertAction) -> Void in
        print("you pressed Yes, please button")
        //topWindow.isHidden = true
        //trying to fix reject 154 sending user to survey from push when app is in forground
        //take user to controller
        DispatchQueue.main.async {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "ccontroller") as! UINavigationController
            topWindow.rootViewController?.present(vc, animated: true, completion: nil)
        }
    })

    let noButton = UIAlertAction(title: get_error(eng_error: "ignore"), style: .default, handler: {(_ action: UIAlertAction) -> Void in
        print("you pressed No, thanks button")
        topWindow.isHidden = true
    })

    alert.addAction(noButton)
    alert.addAction(yesButton)

    topWindow.makeKeyAndVisible()
    topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })
}
O'Neil
  • 3,790
  • 4
  • 16
  • 30
Johny D Good
  • 427
  • 1
  • 8
  • 26

2 Answers2

4

You don't have to fill completion parameter since this parameter has default value which is nil

topWindow.rootViewController?.present(alert, animated: true)

Anyway, if you want to declare completion, you dont need _ in since completion doesn't take any parameter

topWindow.rootViewController?.present(alert, animated: true, completion: { })

or just

topWindow.rootViewController?.present(alert, animated: true) { }
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
0

The compilation in the last line should be nil - the error is about it.

topWindow.rootViewController?.present(alert, animated: true, completion: nil)
Pavel
  • 421
  • 1
  • 4
  • 14