<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="">If your self.init method fails by throwing an exception, the state of the class is essentially unknown. Trying to recover inside the convenience init method by calling another initializer seems problematic since you don't know what the state will be after the second init. Therefor, it's probably more correct to do the exception handling outside of this class like you did in the MakeTestClass function.</div><div class=""><br class=""></div><div class="">But just for the heck of it, I tried taking the second init outside the catch block, and put a flag that something failed. But Swift won't allow any uses of self after the first catch. For example (with errors on the lines the compiler reports):</div><div class=""><br class=""></div><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">convenience</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">init</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">( fromFile file:</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">NSURL</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">, fromBackupFile backupFile:</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">NSURL</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> ) </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">throws</span><br class=""><span style="font-family: Menlo;" class="">{</span><br class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> var</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> firstInitFailed = </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">false</span><br class=""><font face="Menlo" class=""><br class=""></font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> do</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> {</span><br class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">try</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">init</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">( fromFile:file ) </span><br class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> } </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">catch</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> {</span><br class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> do</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> {</span><br class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> <span class="Apple-tab-span" style="white-space:pre">        </span> firstInitFailed = </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">true</span><br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<br class=""><font face="Menlo" class=""><br class=""></font><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> if</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> firstInitFailed == </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">false</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> {</span><br class=""><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>try</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">init</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">( fromFile:backupFile) </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">// 'self' used inside 'catch' block reachable from self.init call</span><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<br class=""><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">} </span><span style="font-variant-ligatures: no-common-ligatures" class="">// 'self' used inside 'catch' block reachable from self.init call</span></div></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div></div><div class="">This might be a bug in the compiler since the error message isn't correct; self isn't referenced in the catch block. Also the error message on the closing brace is puzzling.</div><div class=""><br class=""></div><div class="">In any case, this appears to imply that in Swift you can't do any error recovery that involves self inside of an init method, even if you get an exception from something other than calling self.init. This doesn't seem right if the throw was for something that doesn't affect the object state. For example, if you had a file system call that tried to create 'fromFile' and that call threw an exception. This shouldn't affect anything with class state and should be possible to handle it without affecting object initialization.</div><div class=""><br class=""></div><div class="">The third option is to move this file handling outside of the init method. For example:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">convenience</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">( fromFile file:</span><span style="font-variant-ligatures: no-common-ligatures;" class="">NSURL</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, fromBackupFile backupFile:</span><span style="font-variant-ligatures: no-common-ligatures;" class="">NSURL</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> ) </span><span style="font-variant-ligatures: no-common-ligatures;" class="">throws</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures;" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(fromFile: file)</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures;" class="">openFile</span><span style="font-variant-ligatures: no-common-ligatures" class="">(fromFile: file, fromBackupFile: backupFile)</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> openFile( fromFile file:</span><span style="font-variant-ligatures: no-common-ligatures;" class="">NSURL</span><span style="font-variant-ligatures: no-common-ligatures" class="">, fromBackupFile backupFile:</span><span style="font-variant-ligatures: no-common-ligatures;" class="">NSURL</span><span style="font-variant-ligatures: no-common-ligatures" class=""> )</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">do</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// try to open the fromFile</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> } </span><span style="font-variant-ligatures: no-common-ligatures;" class="">catch</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">do</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// try to open the backupFile</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""></div><div class="">However, this changes the class' init behavior since the designated initializer can't do the file opening/handling. I'm guessing your best bet is going with the separate function to handle this. Or make it a type method.</div><div class=""><br class=""></div><div class="">Doug Hill</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Jul 25, 2016, at 10:46 AM, J.E. Schotsman via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hello,<br class=""><br class="">I have a class that can be initialized from a file.<br class="">I would like to add a convenience init that uses a backup file if necessary.<br class="">Writing a function for this is easy, but I can’t manage to turn this into an init function.<br class=""><br class="">import Foundation<br class=""><br class="">class TestClass<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>init( fromFile file:NSURL ) throws<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>{<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>convenience init( fromFile file:NSURL, fromBackupFile backupFile:NSURL ) throws<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>{<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>do { try self.init( fromFile:file ) }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>catch {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>do { try self.init( fromFile:backupFile ) } // error: 'self' used inside 'catch' block reachable from self.init call<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class="">func MakeTestClass( fromFile file:NSURL, fromBackupFile backupFile:NSURL ) throws -> TestClass<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>do { return try TestClass( fromFile:file ) } <br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>catch { do { return try TestClass( fromFile:backupFile ) } } <br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class="">Any suggestions?<br class=""></div></div></blockquote></div><br class=""></body></html>