Saturday, November 28, 2015

(Im)Proper way to end an iOS app . . .

I am very new to Swift and iOS programming in general, and I had a situation where I wanted to throw an exception to fail an app in an unacceptable situation, and all I knew to do was to call exit(1) in plain old C-fashion.  I put that in my code like on the first day, and I forgot about it.  Since then I have had a lot of bizarre problems with XCode like not running my tests and just logging weird errors like "Failed to bootstrap" and other things.  I finally figured out that was what was causing the errors, and instead I could use:


    fatalError(@autoclosure message: () -> String = default, file: StaticString = default, line: UInt = default)

E.G.

   fatalError("You did something dumb.");

and it accomplishes the same thing (tells me I have done something stupid), but now the debugger stops on that line of code so I remember exactly what I have done.

exit(int), don't use it.

By the way, this is something that only a developer could do, clearly this would not be the way to handle an unexpected user condition.

No comments:

Post a Comment