Created
April 9, 2023 19:20
-
-
Save b3ll/7d7fd4c70cd9e8d05b63a83ca2868ad4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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