alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: { _ in
//Cancel Action
}))
alert.addAction(UIAlertAction(title: "Sign out",
style: UIAlertActionStyle.default,
handler: {(_: UIAlertAction!) in
//Sign out action
}))
第二个声明有这个handler: {(_: UIAlertAction!)
额外的部分,所以我想知道是否有必要,它有什么作用?资料来源:https : //medium.com/swift-india/uialertcontroller-in-swift-22f3c5b1dd68
在编译这个方面
handler: {(_: UIAlertAction!) in
与
handler: { _ in
但是,如果您出于某种原因不在回调中使用警报操作变量
handler: { alertAction in
print(alertAction.title) // e.x
}).
那么做handler: { _ in
总比做好handler: {(_: UIAlertAction!) in
TLDR
通常会给出回调,以防开发人员需要在单击警报操作时执行某些业务,如果此业务需要来自 alertAction 的任何内容,则可以编写以下 2 个选项
1- handler: { alertAction in // recommended
2- handler: { (alertAction:UIAlertAction) in // too long
如果有业务是警报操作的一部分,则可以编写以下 2 个选项
1- handler: { _ in // short and better
2- handler: { (_:UIAlertAction) in // worst ever
此外,如果您在单击警报操作时什么都不做,则供应 nil
alert.addAction(UIAlertAction(title: "Sign out",
style: UIAlertActionStyle.default,
handler:nil)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句