I'm in the process of translating my app to Swift 3. I stumbled upon an issue with using a clean way of setting datasource and delegate for a UICollectionView inside a UITableViewCell, described here.
The code is as follows:
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>
(_ dataSourceDelegate: D, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = row
collectionView.reloadData()}
And it throws a warning, stating:
'protocol<...>' composition syntax is deprecated; join the protocols using '&'
When I accept the suggested solution, it changes the D: protocol<UICollectionViewDataSource, UICollectionViewDelegate> into a D: (UICollectionViewDatasource & UICollectionViewDelegate) call, and instead throws an error:
Expected a type name or protocol composition restricting 'D'
I'd be much obliged if someone with a better understanding of Swift 3 generics than myself could suggest a solution.
No need to use protocol<> because the compiler already knows that. Just join the protocols like this: D: UITableViewDelegate & UITableViewDataSource
setCollectionViewDataSourceDelegate for swift3
extension PollTableViewCell {
func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {
theCollectionView.delegate = dataSourceDelegate
theCollectionView.dataSource = dataSourceDelegate
theCollectionView.tag = row
theCollectionView.setContentOffset(theCollectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
theCollectionView.reloadData()
}
var collectionViewOffset: CGFloat {
set {
theCollectionView.contentOffset.x = newValue
}
get {
return theCollectionView.contentOffset.x
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With