为什么可以用这种奇怪的方式编写 UICollectionViewDiffableDataSource 初始值设定项?

问题描述

在通过 Paul Hudsons 的 tutorial 学习 Swift 时,我遇到了一些奇怪的事情。

UICollectionViewDiffableDataSource 的初始值设定项定义为:

public init(collectionView: UICollectionView,cellProvider: @escaping UICollectionViewDiffableDataSource<SectionIdentifierType,ItemIdentifierType>.CellProvider)

据我所知,没有其他初始化程序。但是,Paul 像这样成功地初始化了它,省略了 cellProvider 参数:

dataSource = UICollectionViewDiffableDataSource<Section,App>(collectionView: collectionView) { collectionView,indexPath,app in
    switch self.sections[indexPath.section].type {
    case "mediumTable":
        return self.configure(MediumTableCell.self,with: app,for: indexPath)
    case "smallTable":
        return self.configure(SmallTableCell.self,for: indexPath)
    default:
        return self.configure(FeaturedCell.self,for: indexPath)
    }
}

与此同时,Ray Wenderlich 的 tutorial 会这样做:

dataSource = UICollectionViewDiffableDataSource<Section,App>(collectionView: collectionView,cellProvider: { (collectionView,app) -> UICollectionViewCell? in
    switch self.sections[indexPath.section].type {
    case "mediumTable":
        return self.configure(MediumTableCell.self,for: indexPath)
    }
})

我试图了解 Paul 的方式背后正在发生什么样的 Swift “魔法”,因为他似乎正在摆脱 cellProvider 的论点,而是做一些时髦的关闭事情。他在这里到底应用了哪些 Swift 规则?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)