<div dir="ltr">Hi all,<div><br></div><div>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:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"><span style="color:rgb(194,52,155)">protocol</span> ViewModelConfigurable</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)">{</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">associatedtype</span> ViewModelType</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53);min-height:13px"> </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">func</span> configure(with viewModel : <span style="color:rgb(0,175,202)">ViewModelType</span>);</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)">}</p></div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(147,201,106);background-color:rgb(40,43,53)"><span style="color:rgb(194,52,155)">class</span><span style="color:rgb(255,255,255)"> TestCell : </span><span style="color:rgb(0,175,202)">UICollectionViewCell</span><span style="color:rgb(255,255,255)">, </span>ViewModelConfigurable</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)">{</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">typealias</span> ViewModelType = <span style="color:rgb(0,175,202)">String</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">func</span> configure(with viewModel: <span style="color:rgb(0,175,202)">String</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(77,191,86)">//todo</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)">}</p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb(40,43,53);min-height:14px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,175,202);background-color:rgb(40,43,53)"><span style="color:rgb(194,52,155)">class</span><span style="color:rgb(255,255,255)"> HeaderFooter : </span>UICollectionReusableView<span style="color:rgb(255,255,255)">, </span><span style="color:rgb(147,201,106)">ViewModelConfigurable</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)">{</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">typealias</span> ViewModelType = <span style="color:rgb(0,175,202)">String</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">func</span> configure(with viewModel: <span style="color:rgb(0,175,202)">String</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(77,191,86)">//todo</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)">}</p></div><div><br></div><div>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:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"><span style="color:rgb(194,52,155)">struct</span> SectionWrapper</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)">{</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">let</span> cellConfigure: (<span style="color:rgb(0,175,202)">UICollectionViewCell</span>, <span style="color:rgb(0,175,202)">Int</span>) -> ()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">let</span> headerConfigure: ((<span style="color:rgb(0,175,202)">UICollectionReusableView</span>) -> ())?</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53);min-height:13px"> </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">init</span><ActualCellType : <span style="color:rgb(0,175,202)">UICollectionViewCell</span> & <span style="color:rgb(147,201,106)">ViewModelConfigurable</span>, HeaderType : <span style="color:rgb(0,175,202)">UICollectionReusableView</span> & <span style="color:rgb(147,201,106)">ViewModelConfigurable</span>>(cellType : <span style="color:rgb(147,201,106)">ActualCellType</span>.Type, cellDataSource : [<span style="color:rgb(147,201,106)">ActualCellType</span>.<span style="color:rgb(0,175,202)">ViewModelType</span>], headerType : (<span style="color:rgb(147,201,106)">HeaderType</span>.Type)?, headerDataSource : (<span style="color:rgb(147,201,106)">HeaderType</span>.<span style="color:rgb(0,175,202)">ViewModelType</span>)?)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">self</span>.<span style="color:rgb(147,201,106)">cellConfigure</span> = { cell, index <span style="color:rgb(194,52,155)">in</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> (cell <span style="color:rgb(194,52,155)">as</span>! <span style="color:rgb(147,201,106)">ActualCellType</span>).<span style="color:rgb(147,201,106)">configure</span>(with: cellDataSource[index])</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53);min-height:13px"> </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">if</span> <span style="color:rgb(194,52,155)">let</span> headerDataSource = headerDataSource</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">self</span>.<span style="color:rgb(147,201,106)">headerConfigure</span> = { header <span style="color:rgb(194,52,155)">in</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> (header <span style="color:rgb(194,52,155)">as</span>! <span style="color:rgb(147,201,106)">HeaderType</span>).<span style="color:rgb(147,201,106)">configure</span>(with: headerDataSource);</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">else</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> <span style="color:rgb(194,52,155)">self</span>.<span style="color:rgb(147,201,106)">headerConfigure</span> = <span style="color:rgb(194,52,155)">nil</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)">}</p></div><div><br></div><div>If I pass nil for headerType and headerDataSource, I get the following error: </div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(77,191,86);background-color:rgb(40,43,53)">error: Swift4Test.playground:86:31: error: generic parameter 'ActualCellType' could not be inferred</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(77,191,86);background-color:rgb(40,43,53)"> let noHeader = SectionWrapper.init(cellType: TestCell.self, cellDataSource: ["Test"], headerType: nil, headerDataSource: nil) //Gives error</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(77,191,86);background-color:rgb(40,43,53)"><br></p></div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"><span style="color:rgb(194,52,155)">let</span> header = <span style="color:rgb(147,201,106)">SectionWrapper</span>.<span style="color:rgb(194,52,155)">init</span>(cellType: <span style="color:rgb(147,201,106)">TestCell</span>.<span style="color:rgb(194,52,155)">self</span>, cellDataSource: [<span style="color:rgb(228,67,71)">"Test"</span>], headerType: <span style="color:rgb(147,201,106)">HeaderFooter</span>.<span style="color:rgb(194,52,155)">self</span>, headerDataSource: <span style="color:rgb(228,67,71)">"Header"</span>) <span style="color:rgb(77,191,86)">//No error</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"><span style="color:rgb(77,191,86)"><br></span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"><span style="color:rgb(194,52,155)">let</span> noHeader = <span style="color:rgb(147,201,106)">SectionWrapper</span>.<span style="color:rgb(194,52,155)">init</span>(cellType: <span style="color:rgb(147,201,106)">TestCell</span>.<span style="color:rgb(194,52,155)">self</span>, cellDataSource: [<span style="color:rgb(228,67,71)">"Test"</span>], headerType: <span style="color:rgb(194,52,155)">nil</span>, headerDataSource: <span style="color:rgb(194,52,155)">nil</span>) <span style="color:rgb(77,191,86)">//Gives error</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);background-color:rgb(40,43,53)"><br></p></div><div><br></div><div>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!</div></div>