【问题标题】:error: Cannot convert value of type '(_) -> ()' to expected argument type '(() -> Void)?'错误:无法将类型“(_)->()”的值转换为预期的参数类型“(()-> Void)?”
【发布时间】:2019-03-12 13:23:46
【问题描述】:

我最近从 swift3 迁移到 swift 4.2。 在构建时,我现在收到以下错误:

无法将类型 '(_) -> ()' 的值转换为预期的参数类型 '(() -> 无效)?'

并且错误出现在以下代码行:

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

【问题讨论】:

    标签: swift swift4.2 xcode10.1


    【解决方案1】:

    您不必填写completion 参数,因为此参数有default value,即nil

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

    无论如何,如果要声明completion,则不需要_ in,因为completion 不带任何参数

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

    或者只是

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

    【讨论】:

      【解决方案2】:

      最后一行的编译应该是 nil - 错误是关于它的。

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

      【讨论】:

        猜你喜欢
        • 2023-02-05
        • 2017-01-13
        • 2023-04-04
        • 1970-01-01
        • 2020-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多