Skip to content

Instantly share code, notes, and snippets.

@b3ll
Created April 9, 2023 19:20
Show Gist options
  • Save b3ll/7d7fd4c70cd9e8d05b63a83ca2868ad4 to your computer and use it in GitHub Desktop.
Save b3ll/7d7fd4c70cd9e8d05b63a83ca2868ad4 to your computer and use it in GitHub Desktop.
import UIKit
protocol DataSource<CellType>: AnyObject {
associatedtype CellType
func createCellFor(index: Int, reusing cell: CellType?) -> CellType
}
class ContainerView<CellType> where CellType: UIView {
weak var dataSource: (any DataSource)?
func fetchCells() {
// Member 'createCellFor' cannot be used on value of type 'any DataSource'; consider using a generic constraint instead
let cell = dataSource?.createCellFor(index: 0, reusing: reuseCell())
}
func reuseCell() -> CellType {
return CellType()
}
}
class SomeClass: NSObject, DataSource {
let thing = ContainerView<UICollectionViewCell>()
override init() {
super.init()
self.thing.dataSource = self
}
func createCellFor(index: Int, reusing cell: CellType?) -> UICollectionViewCell {
return UICollectionViewCell(frame: .zero)
}
typealias CellType = UICollectionViewCell
}
/// is the solution something like:
class ContainerView<CellType, DataSource> where CellType: UIView, DataSource: DataSource, DataSource.CellType == CellType {
/* ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment