swift - 初始化 UIContextualAction class
swift - Initializing the UIContextualAction class
UIContextualAction class 具有具有以下签名的初始化程序。
public convenience init(style: UIContextualAction.Style, title: String?, handler: @escaping UIContextualAction.Handler)
可以使用以下代码片段创建 UIContextualAction class 的实例。
let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}
我不明白的是第三个参数(即handler)传递给[=40=的方式] 初始值设定项。
为什么在传递 delete 参数后,处理函数在单独的花括号内传递而不传递它?
有没有其他方法可以得到相同的输出?
因为第三个参数
let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}
是一个尾随闭包,您希望它在用户点击删除操作时被触发到 运行 一些相关代码,它不是启动警报操作的参数
如果你不需要做任何事情,你也可以选择 nil
let action2 = UIContextualAction(style: .normal, title: "Delete", handler:nil)
https://www.hackingwithswift.com/example-code/language/what-is-trailing-closure-syntax
What is trailing closure syntax in Swift?
这两种写法是一样的:
let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}
let action2 = UIContextualAction(style: .normal, title: "Delete", handler: { (action, view, completion) in
completion(true)
})
你可以看看Trailing Closures的文档:
If you need to pass a closure expression to a function as the
function’s final argument and the closure expression is long, it can
be useful to write it as a trailing closure instead. A trailing
closure is written after the function call’s parentheses, even though
it is still an argument to the function. When you use the trailing
closure syntax, you don’t write the argument label for the closure as
part of the function call.
UIContextualAction class 具有具有以下签名的初始化程序。
public convenience init(style: UIContextualAction.Style, title: String?, handler: @escaping UIContextualAction.Handler)
可以使用以下代码片段创建 UIContextualAction class 的实例。
let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}
我不明白的是第三个参数(即handler)传递给[=40=的方式] 初始值设定项。
为什么在传递 delete 参数后,处理函数在单独的花括号内传递而不传递它?
有没有其他方法可以得到相同的输出?
因为第三个参数
let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}
是一个尾随闭包,您希望它在用户点击删除操作时被触发到 运行 一些相关代码,它不是启动警报操作的参数
如果你不需要做任何事情,你也可以选择 nil
let action2 = UIContextualAction(style: .normal, title: "Delete", handler:nil)
https://www.hackingwithswift.com/example-code/language/what-is-trailing-closure-syntax
What is trailing closure syntax in Swift?
这两种写法是一样的:
let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}
let action2 = UIContextualAction(style: .normal, title: "Delete", handler: { (action, view, completion) in
completion(true)
})
你可以看看Trailing Closures的文档:
If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead. A trailing closure is written after the function call’s parentheses, even though it is still an argument to the function. When you use the trailing closure syntax, you don’t write the argument label for the closure as part of the function call.