如何在 swift 中有效地使用处理程序?

How to efficiently use handlers in swift?

我最近开始学习 Swift 和 Xcode 文档。尽管如此,我在处理程序方面遇到了很多问题,如下面的代码所示:

let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
    // Delete the row from the data source
    self.restaurantNames.remove(at: indexPath.row)
    self.restaurantLocations.remove(at: indexPath.row)
    self.restaurantTypes.remove(at: indexPath.row)
    self.restaurantIsVisited.remove(at: indexPath.row)
    self.restaurantImages.remove(at: indexPath.row)

    self.tableView.deleteRows(at: [indexPath], with: .fade)

    // Call completion handler with true to indicate
    completionHandler(true)
}

我不太明白为什么我正在学习的教程(即appcoda)写的是(action, sourceView, completionHandler)。为了更好地理解,我尝试将此元组更改为 (action, sourceView, completionHandler) 并且它产生了相同的结果,这让我非常惊讶。所以我想问你,这些元组里面应该放什么,有什么作用?

这些是调用 completionHandler 时获得的闭包参数。他们是这样的:

动作:

The object containing information about the selected action.

源视图:

The view in which the action was displayed.

完成处理程序:

The handler block for you to execute after you have performed the action. This block has no return value and takes the following parameter: actionPerformed A Boolean value indicating whether you performed the action. Specify true if you performed the action or false if you were unable to perform the action for some reason.

参考和更多信息here and here