Swift 中的'_ in print()' 是什么意思?
What is the meaning of '_ in print()' in Swift?
嗨,我刚开始学习 Swift。我只是学习 ios 开发的初学者。
func showOkay() {
let title = NSLocalizedString("a title", comment: "")
let message = NSLocalizedString("msg", comment: "")
let cansal = NSLocalizedString("cancel", comment: "")
let ok = NSLocalizedString("ok", comment: "")
let alertController = UIAlertController (title: title, message: message, preferredStyle: .alert)
let cancelAlertAction = UIAlertAction (title : cansal, style : .cancel) {
_ in print("cancel") // i don't understand this line . its just a print or somthing else. why i cant use print here.
}
let okAction = UIAlertAction(title: ok , style : .default) {
_ in print("ok") // i don't understand this line. its just a print or somthing else. why i cant use print here.
}
alertController.addAction(cancelAlertAction)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
@IBAction func btnAction(_ sender: Any) {
showOkay()
}
如果我只使用 print()
他们会给我错误
Cannot convert value of type '() -> ()' to expected argument type '((UIAlertAction) -> Void)?'
此语句使用尾随闭包语法。 {
和 }
之间的东西实际上是一个闭包,它传递给 UIAlertAction
以便稍后在事件发生时调用。调用闭包时,将传递给创建的 UIAlertAction
对象。
let cancelAlertAction = UIAlertAction (title : cansal , style : .cancel) {
_ in print("cancel") \ i don't understand this line . its just a print or somthing else . why i cant use print here.
}
如果您不打算使用警报操作,则需要 _ in
告诉 Swift 您正在忽略 UIAlertAction
并且什么也不做。你是说,我知道有一个参数,但我忽略了它。
如果您没有指定 _ in
,Swift 会推断您的闭包类型为 () -> ()
类型,这意味着它什么都不接受,什么也不产生。这与您期望提供的闭包类型不匹配,即 (UIAlertAction) -> Void
(不带 UIAlertAction
和 returns)。
一般是这样写的:
let cancelAlertAction = UIAlertAction (title : cansal , style : .cancel) { _ in
print("cancel")
}
更清楚地表明 _ in
是闭包参数语法,与 print
语句没有直接关系。
嗨,我刚开始学习 Swift。我只是学习 ios 开发的初学者。
func showOkay() {
let title = NSLocalizedString("a title", comment: "")
let message = NSLocalizedString("msg", comment: "")
let cansal = NSLocalizedString("cancel", comment: "")
let ok = NSLocalizedString("ok", comment: "")
let alertController = UIAlertController (title: title, message: message, preferredStyle: .alert)
let cancelAlertAction = UIAlertAction (title : cansal, style : .cancel) {
_ in print("cancel") // i don't understand this line . its just a print or somthing else. why i cant use print here.
}
let okAction = UIAlertAction(title: ok , style : .default) {
_ in print("ok") // i don't understand this line. its just a print or somthing else. why i cant use print here.
}
alertController.addAction(cancelAlertAction)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
@IBAction func btnAction(_ sender: Any) {
showOkay()
}
如果我只使用 print()
他们会给我错误
Cannot convert value of type '() -> ()' to expected argument type '((UIAlertAction) -> Void)?'
此语句使用尾随闭包语法。 {
和 }
之间的东西实际上是一个闭包,它传递给 UIAlertAction
以便稍后在事件发生时调用。调用闭包时,将传递给创建的 UIAlertAction
对象。
let cancelAlertAction = UIAlertAction (title : cansal , style : .cancel) {
_ in print("cancel") \ i don't understand this line . its just a print or somthing else . why i cant use print here.
}
如果您不打算使用警报操作,则需要 _ in
告诉 Swift 您正在忽略 UIAlertAction
并且什么也不做。你是说,我知道有一个参数,但我忽略了它。
如果您没有指定 _ in
,Swift 会推断您的闭包类型为 () -> ()
类型,这意味着它什么都不接受,什么也不产生。这与您期望提供的闭包类型不匹配,即 (UIAlertAction) -> Void
(不带 UIAlertAction
和 returns)。
一般是这样写的:
let cancelAlertAction = UIAlertAction (title : cansal , style : .cancel) { _ in
print("cancel")
}
更清楚地表明 _ in
是闭包参数语法,与 print
语句没有直接关系。