[swift-users] Optional generics question
Joel Loeshelle
jml5qh at virginia.edu
Thu Jun 8 13:11:06 CDT 2017
Hi all,
Is there any support for having an optional type in an initializer? My use
case is I want to have a struct SectionWrapper that includes information
about a Collection View Section. So you can pass the CellType, Cell Data
Source, and an Optional HeaderType and HeaderDataSource if there is a
Section Header. Here's the code. I first have a protocol
ViewModelConfigurable that the Cell and Header subclasses conform to:
protocol ViewModelConfigurable
{
associatedtype ViewModelType
func configure(with viewModel : ViewModelType);
}
class TestCell : UICollectionViewCell, ViewModelConfigurable
{
typealias ViewModelType = String
func configure(with viewModel: String) {
//todo
}
}
class HeaderFooter : UICollectionReusableView, ViewModelConfigurable
{
typealias ViewModelType = String
func configure(with viewModel: String) {
//todo
}
}
I then have a SectionWrapper struct where I want to initialize with a
concrete UICollectionViewCell type that conforms to ViewModelType and pass
the associated dataSource which must be an array of ViewModelType. I also
want to have the ability to pass an optional UICollectionReusableView type
that conforms to ViewModelType with the associated dataSource. You could
then pass nil if this section does not have a Header. See below:
struct SectionWrapper
{
let cellConfigure: (UICollectionViewCell, Int) -> ()
let headerConfigure: ((UICollectionReusableView) -> ())?
init<ActualCellType : UICollectionViewCell & ViewModelConfigurable,
HeaderType : UICollectionReusableView & ViewModelConfigurable>(cellType :
ActualCellType.Type, cellDataSource : [ActualCellType.ViewModelType],
headerType : (HeaderType.Type)?, headerDataSource : (HeaderType.
ViewModelType)?)
{
self.cellConfigure = { cell, index in
(cell as! ActualCellType).configure(with: cellDataSource[index])
}
if let headerDataSource = headerDataSource
{
self.headerConfigure = { header in
(header as! HeaderType).configure(with: headerDataSource);
}
}
else
{
self.headerConfigure = nil
}
}
}
If I pass nil for headerType and headerDataSource, I get the following
error:
error: Swift4Test.playground:86:31: error: generic parameter
'ActualCellType' could not be inferred
let noHeader = SectionWrapper.init(cellType: TestCell.self,
cellDataSource: ["Test"], headerType: nil, headerDataSource: nil) //Gives
error
let header = SectionWrapper.init(cellType: TestCell.self, cellDataSource: [
"Test"], headerType: HeaderFooter.self, headerDataSource: "Header") //No
error
let noHeader = SectionWrapper.init(cellType: TestCell.self, cellDataSource:
["Test"], headerType: nil, headerDataSource: nil) //Gives error
I have also attached the Playground that I'm working through. Please let me
know if you have any questions and thanks in advance for your help!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170608/e0953f0e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Swift4Test.playground.zip
Type: application/zip
Size: 7999 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170608/e0953f0e/attachment.zip>
More information about the swift-users
mailing list