While learning Swift via Paul Hudsons's tutorial, I came across something strange.
The initializer of UICollectionViewDiffableDataSource is defined as:
public init(collectionView: UICollectionView, cellProvider: @escaping UICollectionViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType>.CellProvider)
There is no other initializer as far as I can tell. However, Paul successfully initializes it like this, leaving out the cellProvider argument:
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)
}
}
Meanwhile, Ray Wenderlich's tutorial would do it like this:
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)
}
})
I'm trying to understand what kind of Swift "magic" is going on behind Paul's way, as he seems to be getting away with dropping the cellProvider argument and instead doing some funky closure thing. Which Swift rules has he been applying here exactly?