× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

What's the easiest way to make swift crash?

Antonio Bello
Jul 06, 2015
<p>You can use the forced unwrapping operator on a nil optional variable:</p> <pre><code>let number: Int? = nil let val = number! </code></pre> <p>That should throw an exception like this:</p> <blockquote> <p>fatal error: unexpectedly found nil while unwrapping an Optional value</p> </blockquote> <p>However you can also use a more elegant way to make your app crash, using the <code>fatalError</code> global function, which stops the program execution - but it accepts some parameters that can be useful depending on what you are trying to achieve:</p> <pre><code>@noreturn func fatalError(@autoclosure message: () -&gt; String = default, file: StaticString = default, line: UWord = default) </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31110343/What's%20the%20easiest%20way%20to%20make%20swift%20crash?/31110433">Stack Overflow</a>.</p>
comments powered by Disqus