gpt4 book ai didi

swift - 初始化 UIContextualAction 类

转载 作者:行者123 更新时间:2023-11-28 10:30:36 25 4
gpt4 key购买 nike

UIContextualAction 类具有具有以下签名的初始化程序。

public convenience init(style: UIContextualAction.Style, title: String?, handler: @escaping UIContextualAction.Handler)

可以使用以下代码片段创建 UIContextualAction 类的实例。

let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}

我不明白的是第三个参数(即 handler)传递给类初始值设定项的方式。

为什么在传递delete 参数值后处理函数在单独的大括号内传递而不传递它?

有没有其他方法可以得到相同的输出?

最佳答案

这两种写法是一样的:

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.

关于swift - 初始化 UIContextualAction 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56174917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com