error: Cannot convert value of type '(_) -> ()' to expected argument type '(() -> Void)?'
error: Cannot convert value of type '(_) -> ()' to expected argument type '(() -> Void)?'
我最近从 swift3 迁移到 swift 4.2。
现在构建时出现以下错误:
Cannot convert value of type '(_) -> ()' to expected argument type
'(() -> 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 })
}
最后一行的编译应该是 nil - 错误是关于它的。
topWindow.rootViewController?.present(alert, animated: true, completion: nil)
您不必填写 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) { }
我最近从 swift3 迁移到 swift 4.2。 现在构建时出现以下错误:
Cannot convert value of type '(_) -> ()' to expected argument type '(() -> 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 })
}
最后一行的编译应该是 nil - 错误是关于它的。
topWindow.rootViewController?.present(alert, animated: true, completion: nil)
您不必填写 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) { }