使作为参数传递给另一个函数的函数可选?
Make a function that is passed as an argument to another function optional?
我有一个 UIAlertController
的扩展,我用它来在我的应用程序中显示服务条款的弹出窗口。
我想要这个弹出窗口的两个版本:一个是用户可以接受或拒绝 ToS(将在第一次使用应用程序时显示),另一个是用户可以阅读它们并然后关闭弹出窗口(随时显示在设置屏幕中)。
这两个弹出窗口非常相似,所以与其重写同一个函数两次,不如创建另一个调用 termsOfServiceAlert()
并修改参数的函数。但是,由于用户只能在调用 termsOfServiceAlternativeAlert()
时关闭 ToS,因此我需要将 accept
和 decline
参数设为 可选 .我知道如何对普通变量执行此操作,但我找不到方法使其适用于作为参数传递的函数。
这是代码片段:
extension UIAlertController {
static func termsOfServiceAlert(
title: String,
message: String?,
acceptText: String,
accept: @escaping ()->Void,
declineText: String,
decline: @escaping ()->Void) -> UIAlertController {
/* set up alert */
let acceptTermsHandler: (UIAlertAction) -> Void = { (alertAction in
accept()
}
let declineTermsHandler: (UIAlertAction) -> Void = { (alertAction in
decline()
}
let accept = "Accept"
alert.addAction(UIAlertAction(title: accept, style: .default, handler: acceptTermsHandler
let decline = "Decline"
alert.addAction(UIAlertAction(title: decline, style: .default, handler: declineTermsHandler
return alert
}
static func termsOfServiceAlternativeAlert(message: String, dismiss: String) -> UIAlertController {
/* ERROR - Nil is not compatible with expected argument type '() -> Void */
return termsOfService(
message: message,
acceptText: dismiss,
accept: nil,
declineText: nil,
decline: nil)
}
}
您需要将这些参数作为 optional
,然后作为 nil
传递。这是修复,
extension UIAlertController {
static func termsOfServiceAlert(
title: String,
message: String?,
acceptText: String,
accept: (()->Void)?,
declineText: String?,
decline: (()->Void)?) -> UIAlertController {
/* set up alert */
let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
let acceptTermsHandler: (UIAlertAction) -> Void = { alertAction in
accept?()
}
let declineTermsHandler: (UIAlertAction) -> Void = { alertAction in
decline?()
}
alert.addAction(UIAlertAction(title: "Accept", style: .default, handler: acceptTermsHandler))
alert.addAction(UIAlertAction(title: "Decline", style: .default, handler: declineTermsHandler))
return alert
}
static func termsOfServiceAlternativeAlert(message: String, dismiss: String) -> UIAlertController {
/* ERROR - Nil is not compatible with expected argument type '() -> Void */
return termsOfServiceAlert(
title: "",
message: message,
acceptText: dismiss,
accept: nil,
declineText: nil,
decline: nil)
}
}
我有一个 UIAlertController
的扩展,我用它来在我的应用程序中显示服务条款的弹出窗口。
我想要这个弹出窗口的两个版本:一个是用户可以接受或拒绝 ToS(将在第一次使用应用程序时显示),另一个是用户可以阅读它们并然后关闭弹出窗口(随时显示在设置屏幕中)。
这两个弹出窗口非常相似,所以与其重写同一个函数两次,不如创建另一个调用 termsOfServiceAlert()
并修改参数的函数。但是,由于用户只能在调用 termsOfServiceAlternativeAlert()
时关闭 ToS,因此我需要将 accept
和 decline
参数设为 可选 .我知道如何对普通变量执行此操作,但我找不到方法使其适用于作为参数传递的函数。
这是代码片段:
extension UIAlertController {
static func termsOfServiceAlert(
title: String,
message: String?,
acceptText: String,
accept: @escaping ()->Void,
declineText: String,
decline: @escaping ()->Void) -> UIAlertController {
/* set up alert */
let acceptTermsHandler: (UIAlertAction) -> Void = { (alertAction in
accept()
}
let declineTermsHandler: (UIAlertAction) -> Void = { (alertAction in
decline()
}
let accept = "Accept"
alert.addAction(UIAlertAction(title: accept, style: .default, handler: acceptTermsHandler
let decline = "Decline"
alert.addAction(UIAlertAction(title: decline, style: .default, handler: declineTermsHandler
return alert
}
static func termsOfServiceAlternativeAlert(message: String, dismiss: String) -> UIAlertController {
/* ERROR - Nil is not compatible with expected argument type '() -> Void */
return termsOfService(
message: message,
acceptText: dismiss,
accept: nil,
declineText: nil,
decline: nil)
}
}
您需要将这些参数作为 optional
,然后作为 nil
传递。这是修复,
extension UIAlertController {
static func termsOfServiceAlert(
title: String,
message: String?,
acceptText: String,
accept: (()->Void)?,
declineText: String?,
decline: (()->Void)?) -> UIAlertController {
/* set up alert */
let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
let acceptTermsHandler: (UIAlertAction) -> Void = { alertAction in
accept?()
}
let declineTermsHandler: (UIAlertAction) -> Void = { alertAction in
decline?()
}
alert.addAction(UIAlertAction(title: "Accept", style: .default, handler: acceptTermsHandler))
alert.addAction(UIAlertAction(title: "Decline", style: .default, handler: declineTermsHandler))
return alert
}
static func termsOfServiceAlternativeAlert(message: String, dismiss: String) -> UIAlertController {
/* ERROR - Nil is not compatible with expected argument type '() -> Void */
return termsOfServiceAlert(
title: "",
message: message,
acceptText: dismiss,
accept: nil,
declineText: nil,
decline: nil)
}
}