ошибка: не удается преобразовать значение типа '(_) - ›()' в ожидаемый тип аргумента '(() -› Void)?'

Недавно я перешел с swift3 на swift 4.2. При сборке я получаю следующую ошибку:

Невозможно преобразовать значение типа '(_) -> ()' в ожидаемый тип аргумента '(() -> Void)?'

и ошибка появляется в следующей строке кода:

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

Я не совсем уверен, как исправить эту ошибку. Любая оказанная помощь будет принята с благодарностью

    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 })
}

person Johny D Good    schedule 12.03.2019    source источник


Ответы (2)


Параметр completion заполнять не нужно, поскольку этот параметр имеет значение по умолчанию что nil

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

В любом случае, если вы хотите объявить completion, вам не нужно _ in, поскольку completion не принимает никаких параметров

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

или просто

topWindow.rootViewController?.present(alert, animated: true) { }
person Robert Dresler    schedule 12.03.2019

Компиляция в последней строке должна быть нулевой - ошибка именно в этом.

topWindow.rootViewController?.present(alert, animated: true, completion: nil)
person Pavel    schedule 12.03.2019