<div dir="ltr"><font face="monospace, monospace">initWithCoder(_:)</font> is exactly such an example you can solve using initialization closures and/or lazy vars I mentioned earlier.<div><br><div>The only limitation would be that properties must be initialized independently from each other.</div></div><div>If they really depend on each other then using a struct for them probably makes sense since that can be initialized as a whole.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 16, 2015 at 3:00 AM, Colin Cornaby <span dir="ltr">&lt;<a href="mailto:colin.cornaby@mac.com" target="_blank">colin.cornaby@mac.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">In UIKIt a view can have two primary methods of initialization: initWithCoder and initWithFrame. initWithCoder is special in that it will not call up to initWithFrame, while any other initializer will use initWthFrame as it’s base initializer.<div><br></div><div>A common issue is that you might want to set defaults based on runtime information, so you define a common “setup” function that each one of the initializers calls. Again, I haven’t run into this case yet in Swift, but my understanding is that this would not work because the initializers would not be able to call a function on themselves until initialization was complete.</div><div><br></div><div>There is one possible way out. NIB outlets are declared implicitly unwrapped to work around this exact problem. A NIB can’t be loaded until after the object’s initialization is complete, and therefore the outlet properties of the object can’t be populated at init time. Any properties that need to have common setup between the two initializers could be declared as implicitly unwrapped. You’d lose some safety, but any issues could probably be quickly caught. This would work around the problem of needing to populate all the properties of the object before calling to the super’s init.<br><div><br><div><blockquote type="cite"><div><div class="h5"><div>On Dec 15, 2015, at 5:51 PM, Marc Knaup via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br></div></div><div><div><div class="h5"><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Although I understand your concerns (Charles and Ross) I think an extensive example is still necessary to properly discuss this proposal. We&#39;d have to figure out what exactly causes the limitation, if there are alternatives and if there is no alternative which would render the change unnecessary.<div>So far this is a very theoretical discussion.<div><br></div></div></div><div class="gmail_extra" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br><div class="gmail_quote">On Wed, Dec 16, 2015 at 2:48 AM, Charles Srstka<span> </span><span dir="ltr">&lt;<a href="mailto:cocoadev@charlessoft.com" target="_blank">cocoadev@charlessoft.com</a>&gt;</span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><span><blockquote type="cite">On Dec 15, 2015, at 7:06 PM, Marc Knaup &lt;<a href="mailto:marc@knaup.koeln" target="_blank">marc@knaup.koeln</a>&gt; wrote:<br></blockquote><div><blockquote type="cite"><br><div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">In its current state with the initial example -1 from me for the proposal.</span><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">The example could easily be written like this:</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><font face="monospace, monospace">class FooView: UIView {</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">   <span> </span>var property = 4</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">   <span> </span>init() {</font></div><div><span style="font-family:monospace,monospace">       <span> </span>super.init()</span><br></div><div><font face="monospace, monospace">   <span> </span>}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">   <span> </span>init(frame: CGRect) {</font></div><div><font face="monospace, monospace">       <span> </span>super.init(frame)</font></div><div><font face="monospace, monospace">   <span> </span>}</font></div><div><font face="monospace, monospace">}</font></div></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">In cases where the initially value is computed in a complex way a closure can be used:</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><font face="monospace, monospace">class FooView: UIView {</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">   <span> </span>var property: Int = {</font></div><div><font face="monospace, monospace">       <span> </span>// some complicated computation</font></div><div><font face="monospace, monospace">       <span> </span>return value</font></div><div><font face="monospace, monospace">   <span> </span>}()</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">   <span> </span>init() {</font></div><div><span style="font-family:monospace,monospace">       <span> </span>super.init()</span><br></div><div><font face="monospace, monospace">   <span> </span>}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">   <span> </span>init(frame: CGRect) {</font></div><div><font face="monospace, monospace">       <span> </span>super.init(frame)</font></div><div><font face="monospace, monospace">   <span> </span>}</font></div><div><font face="monospace, monospace">}</font></div></div></div></blockquote></div><br></span><div>That works for simplistic cases such as this. However, if you have a large number of properties that all need to be set, and if their values are all interconnected in some way, dependent on some of the initializer’s parameters, or dependent on some value computed at runtime, this pattern cannot accomplish the task cleanly.</div><span><font color="#888888"><div><br></div><div>Charles</div><div><br></div></font></span></div></blockquote></div><br></div></div></div><span class=""><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=OFgvmg1J6naJevMotmPmRgzA4p1gcL5s2O89Xj3c0DSwsyiayQ-2BU6ptgtkJ9UKnj0wyGanhKGGyPBy3QQ22hnbRPNNkYkuJY1fdcwonbjByE3Syg5YUecx8bdsv-2FOlvJ88j7KyOVN97JYAaPdMbXDLzbR-2B7cFhFhPAoX5utdt2eyDWi-2F2U8VVVj4949FbbRiurFg37wZzLUD9smjna-2BblETPXRn77p661q-2FiIV-2FGb-2F8-3D" alt="" width="1" height="1" border="0" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important"><span> </span>_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-evolution mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-evolution@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span></div></blockquote></div><br></div></div></div></blockquote></div><br></div>