如何在协议扩展中设置委托

How to set delegate in a protocol extension

我有多个显示相同类型单元格的视图控制器。我想在这样的协议扩展中设置委托:

class ProductsViewController: UIViewController, ProductShowcase {
    //other properties
    @IBOutlet weak var productCollectionView: UICollectionView!
    var dataSource: DataSource!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupDataSource()

        setupCollectionView()
    }

    func didSelectProduct(product: Product) {
        print(product)
    }

    //other functions
}

protocol ProductShowcase: UICollectionViewDelegate {
    var dataSource: DataSource! { get set }
    var productCollectionView: UICollectionView! { get }

    func didSelectProduct(product: Product)
}

extension ProductShowcase {
    func setupCollectionView() {
        productCollectionView.registerClass(ProductCollectionViewCell.self, forCellWithReuseIdentifier: "productCell")
        productCollectionView.dataSource = dataSource
        print(self) //prints ProductsViewController
        productCollectionView.delegate = self // 
        print(productCollectionView.delegate) //prints optional ProductsViewController
    }
}

extension ProductShowcase {
    //this delegate method is not called
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        didSelectProduct(dataSource.dataObjects[indexPath.row])
    }
}

didSelectItemAtIndexPathProductsViewController 中实现时,它会被调用。是不是我遗漏了什么或者这是一种错误的方法?

这是一个 Objective-C 互操作性限制。您不能像您想要的那样在协议扩展中实现具有可选功能的协议(来自 Objective-C 类型 UIKit 控件的委托和数据源等的协议)。您可以只使用如下协议的默认实现:

// No, @objc in the front of protocol. (i.e. objc-type protocol)
protocol X {
}