swift - 为什么可以用这种奇怪的方式编写 UICollectionViewDiffableDataSource 初始化程序?
问题描述
在通过 Paul Hudsons 的教程学习 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, with: app, for: indexPath)
default:
return self.configure(FeaturedCell.self, with: app, for: indexPath)
}
}
同时,Ray Wenderlich 的教程会这样做:
dataSource = UICollectionViewDiffableDataSource<Section, App>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, app) -> UICollectionViewCell? 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, with: app, for: indexPath)
default:
return self.configure(FeaturedCell.self, with: app, for: indexPath)
}
})
我试图了解 Paul 的方式背后发生了什么样的 Swift “魔法”,因为他似乎放弃了 cellProvider 参数,而是做了一些时髦的闭包事情。他在这里应用了哪些 Swift 规则?
解决方案
在您的情况下,您会看到两种使用不同语法编写相同内容的方法。
在这里,您使用两个参数 collectionView 和 cellProvider 以预期的方式使用所有参数。
dataSource = UICollectionViewDiffableDataSource<Section, App>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, app) -> UICollectionViewCell? 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, with: app, for: indexPath)
default:
return self.configure(FeaturedCell.self, with: app, for: indexPath)
}
})
在这里,您使用第二个参数 (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, with: app, for: indexPath)
default:
return self.configure(FeaturedCell.self, with: app, for: indexPath)
}
}
看到这在两个示例中都是相同的:
{ 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, with: app, for: indexPath)
default:
return self.configure(FeaturedCell.self, with: app, for: indexPath)
}
更多关于尾随闭包或什么是尾随闭包语法?
推荐阅读
- android - 将 C++ 库从 Windows 上的 C++ 源代码移植到 android
- visual-studio - Visual Studio 代码覆盖率未突出显示覆盖的代码
- reactjs - RxDB - 使用现有的本地数据库
- php - 金额格式不正确,格式正确
- html - 预期的表或查询集,而不是 str
- javascript - 如何在将溢出用作滚动时动态更改元素的水平位置?
- reactjs - 使用 useRef react hook 时如何使用 jest 提交表单?
- postgresql - Postgres、大型数组和 MVCC
- c# - 无法加载文件或程序集以供项目参考
- django - 如何在 django 设置文件中添加相同站点无选项