<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><font size="4" class="">Hi all,</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">recently I stumbled upon an interesting and slightly confusing aspect of our favorite language. </font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">I had defined a <font face="Consolas" class="">Cache</font> class to</font><span class="" style="font-size: large;"> con</span><span class="" style="font-size: large;">form to </span><font face="Consolas" class="">NSCoding</font> and <font size="4" class="">inherit from <font face="Consolas" class="">NSObject</font></font><span class="" style="font-size: large;">. My class’s initializer was declared </span><font face="Consolas" class="">init?(entries: [Entry]? = nil)</font><span class="" style="font-size: large;"> at first, giving an option to pre-populate it when called from the </span><font face="Consolas" class="">NSCoding</font><span class="" style="font-size: large;">-conformant initializer but also simply start from scratch in my program. Failability is a valuable feature since the class depends on external resources that could be unavailable. Of course there are other means to model this, but a failable initializer is the most elegant one, I think.</span></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">Later on, I decided to make the <font face="Consolas" class="">Entry</font> class <font face="Consolas" class="">private</font> to my <span class="" style="font-family: Consolas;">Cache</span> class.</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">Now I had to split the initializer in a private <font face="Consolas" class="">init?(entries: [Entry]?)</font> and an <font face="Consolas" class="">internal</font> or <font face="Consolas" class="">public convenience init?()</font>.</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">I’ll abstract a bit now:</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><br class=""></div><div class=""><font size="4" class="">First version:</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font face="Consolas" size="4" class="">public class A: NSObject {</font><div class=""><font face="Consolas" size="4" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>public class X { }</font></div><div class=""><font face="Consolas" size="4" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>public init?(x: X? = nil) { }</font></div><font face="Consolas" size="4" class="">}</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">— all good. I can use it like <font face="Consolas" class="">let a = A()</font> in my program.</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><span class="" style="font-size: large;">Second version:</span></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font face="Consolas" size="4" class="">public class B: NSObject {</font><div class=""><font face="Consolas" size="4" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>private class X { }</font></div><div class=""><font face="Consolas" size="4" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>private init?(x: X?) { }</font></div><div class=""><font face="Consolas" size="4" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>public convenience override init?() { self.init(x: nil) }<span class="Apple-tab-span" style="white-space: pre;">        </span>// ERROR</font></div><font face="Consolas" size="4" class="">}</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">Now the compiler complains "<i class="">failable initializer 'init()' cannot override a non-failable initializer</i>" with the overridden initializer being the <font face="Consolas" class="">public init()</font> in NSObject. This is the same in Swift 2 and 3.</font></div><div class=""><font size="4" class="">Omitting the </font><font face="Consolas" size="4" class="">override</font><font size="4" class=""> does not work, the compiler now says “<i class="">overriding declaration requires an 'override’ keyword</i>", so it does count as overriding anyway. Suggested Fix-it is inserting </font><font face="Consolas" size="4" class="">override</font><font size="4" class="">, giving the other error.</font></div><br class=""><font size="4" class="">How comes I can effectively declare an initializer </font><font face="Consolas" size="4" class="">A.init?()</font><font size="4" class=""> without the conflict but not </font><font face="Consolas" size="4" class="">B.init?()</font><font size="4" class=""> ?</font><br class=""><br class=""><font size="4" class="">And: Why at all am I not allowed to override a non-failable initializer with a failable one? The opposite is legal: I can override a failable initializer with a non-failable, which even requires using a forced </font><font face="Consolas" size="4" class="">super.init()!</font><font size="4" class=""> and thus introduces the risk of a runtime error. I always have a bad feeling when I use </font><font face="Consolas" size="4" class="">!</font><font size="4" class="">, so I try to avoid it whenever possible. I would even accept to get a compiler warning ;-)</font><div class=""><font size="4" class=""><br class=""></font><div class=""><font size="4" class="">To me, letting the subclass have the failable initializer feels more natural since an extension of functionality introduces <i class="">more</i> chance of failure. But maybe I am missing something here – explanation greatly appreciated.</font><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">But I found a workaround. My class now looks like this:</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><div class=""><font face="Consolas" size="4" class="">public class C: NSObject {</font><div class=""><font face="Consolas" size="4" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>private class X { }</font></div><div class=""><font face="Consolas" size="4" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>private init?(x: X?) { }</font></div><div class=""><font face="Consolas" size="4" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>public convenience init?(_: Void) { self.init(x: nil) }<span class="Apple-tab-span" style="white-space: pre;">                </span>// NO ERROR</font></div><font face="Consolas" size="4" class="">}</font></div></div><div class=""><br class=""></div><div class=""><font size="4" class="">Now I can use it like let </font><font face="Consolas" size="4" class="">c = C(())</font><font size="4" class=""> or even </font><font face="Consolas" size="4" class="">let c = C()</font><font size="4" class=""> – which is exactly what I intended at first.</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">The fact that the new declaration does not generate an error message seems (kind of) legal since it (somehow) differs from the superclass initializer's signature. Nevertheless, using a nameless </font><font face="Consolas" size="4" class="">Void</font><font size="4" class=""> parameter at all and then even omitting it completely in the 2nd version of the call feels a bit strange… But the end justifies the means, I suppose. Elegance is almost preserved.</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">What do you think?</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">Sincerely,</font></div><div class=""><font size="4" class="">Stefan</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">PS: I already posted this question on </font><a href="http://stackoverflow.com/q/38311365/1950945" class="">Stack Overflow</a><font size="4" class="">, but for the more philosophical aspects / underpinnings, it is not the right medium — you are obliged to ask a concrete question there (<i class="">How to?</i>, not <i class="">Why?</i>).</font></div></div></div></body></html>