Swift类型转换错误:无法将'(_) -> Void?'转为'(() -> Void)?'求助
Hey there, let's break down and fix this type mismatch error you're hitting.
What's causing this error?
This message tells you that you're passing a closure designed to accept one parameter (the (_) -> Void? part) to a method that expects a closure with no parameters ((() -> Void)?). The signature of your closure doesn't match what the method is looking for.
Looking at your code snippet, the issue is almost certainly in the truncated deselectItem(...) call at the end. You likely added an unnecessary parameter placeholder (like _ in) in the completion closure, which creates a closure that expects an input argument—even though the method's completion handler doesn't take any parameters.
How to fix it
Let's adjust your code to match the expected closure signature:
Corrected full code snippet
func deselectSelectedCells() { if let start = startDateIndexPath { var section = start.section var item = start.item + 1 if let cell = collectionView?.cellForItem(at: start) as? AirbnbDatePickerCell { cell.type.remove([.InBetweenDate, .SelectedStartDate, .SelectedEndDate, .Selected]) cell.configureCell() // Fixed closure: removed unnecessary parameter placeholder collectionView?.deselectItem(at: start, animated: true, completion: { // Your completion logic (if any) goes here }) } } }
If you prefer Swift's cleaner trailing closure syntax, you can also write it like this:
collectionView?.deselectItem(at: start, animated: true) { // Your completion logic (if any) goes here }
Why this works
By removing the _ in from your closure, you're creating a closure that matches the expected type (() -> Void)?—a closure that takes no parameters and returns nothing. This aligns perfectly with what the deselectItem method expects for its completion handler.
内容的提问来源于stack exchange,提问作者Mohamed Rasool




