<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Thanks a lot, Jordan.<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">在 2017年12月15日,上午2:08,Jordan Rose &lt;<a href="mailto:jordan_rose@apple.com" class="">jordan_rose@apple.com</a>&gt; 写道:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Dec 13, 2017, at 23:05, 吴君恺 via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Hello community,&nbsp;</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>I just encountered a pretty weird behavior when I subclass &nbsp;`UINavigationController` without overriding any initializers.</div><div class=""><br class=""></div>Simple Code:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">import UIKit</font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 14px;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class=""><br class=""></font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">class MyViewController: UINavigationController {</font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 15px;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">&nbsp;&nbsp;</font><br class="webkit-block-placeholder"></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">&nbsp; let value: Int = {</font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">&nbsp; &nbsp; print("member init")</font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">&nbsp; &nbsp; return 3</font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">&nbsp; }()</font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">}</font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 14px;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class=""><br class=""></font></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Menlo" style="background-color: rgb(255, 255, 255); font-size: 12px;" color="#0433ff" class="">let _ = MyViewController(rootViewController: UIViewController())</font></div></div></blockquote><div class=""><br class=""></div><div class="">output is:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; background-color: rgb(255, 255, 255);" class=""><b class="">member init</b></span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; background-color: rgb(255, 255, 255);" class=""><b class="">member init</b></span></div></div></blockquote><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; background-color: rgb(255, 255, 255);" class=""><b class=""><br class=""></b></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="background-color: rgb(255, 255, 255);" class="">In fact any member variables declared in this subclass is initialized twice but deinitialized only once.</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="background-color: rgb(255, 255, 255);" class="">This phenomenon only appears when using `init(rootViewController:)`.&nbsp;</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="background-color: rgb(255, 255, 255);" class="">CMIW, It looks like Swift somehow treat this initializer as a designated initializer, rather than a convenience one.&nbsp;</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="background-color: rgb(255, 255, 255);" class="">Any Ideas?</span></div></div></div></blockquote><br class=""></div><div class="">Your analysis is correct. The problem is that UINavigationController's header file doesn't properly declare which initializers are designated and which are convenience. Fortunately, you're not the first to notice this; it's tracked by the UIKit folks as&nbsp;<a href="rdar://problem/27255526" class="">rdar://problem/27255526</a>. I'll poke them about it.</div><div class=""><br class=""></div><div class="">Thanks for bringing it up!</div><div class="">Jordan</div><br class=""></div></div></blockquote></div><br class=""></body></html>