Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Assertions behaviour in production applications

I'm reading the Assertions section in the Swift e-book and it looks like assertions work very similarly to their Objective-C counterparts. However, nowhere in the docs can I find anything about runtime behaviour while running as a production app. Objective-C's NSAssert promises never to terminate a production application as a result of an assertion failure. Is it the same case in Swift?

like image 248
Stavash Avatar asked Jun 04 '14 12:06

Stavash


2 Answers

Based upon the language Apple use in their documentation, I'd say assertions are ignored in a production environment.

If your code triggers an assertion while running in a debug environment, such as when you build and run an app in Xcode, you can see exactly where the invalid state occurred and query the state of your app at the time that the assertion was triggered. An assertion also lets you provide a suitable debug message as to the nature of the assert.

And in the "Note" block:

Assertions cause your app to terminate and are not a substitute for designing your code in such a way that invalid conditions are unlikely to arise. Nonetheless, in situations where invalid conditions are possible, an assertion is an effective way to ensure that such conditions are highlighted and noticed during development, before your app is published.

like image 150
FreeAsInBeer Avatar answered Sep 23 '22 03:09

FreeAsInBeer


The difference between debug and release is the difference between compiler arguments. The most likely answer is that there will be some special compiler settings (similar to -ea in Java).

EDIT
The Swift compiler has an argument called -assert-config

-assert-config Specify the assert_configuration replacement. Possible values are Debug, Release, Replacement.

In Release, the assertions are ignored. Not sure about the difference between Debug and Replacement.

enter image description here

like image 34
Sulthan Avatar answered Sep 22 '22 03:09

Sulthan