9

I have several assert(condition, "message") statements in my project.

They are used to check invariant conditions during development. I thought they would be ignored in production/release build (as stated in this answer). They are not. Instead they cause crashes during TestFlight testing. When I comment asserts the app does not crash. Something usually gets wrong a bit but it does not crash.

Can it be something with my build settings?

All my archive schemes use release configuration:

enter image description here

The asserts are in Cocoa Touch Framework project, that is used from custom keyboard extension.

All the targets in all projects (Cocoa Touch Framework, and the main project with keyboard extension target) have these Build Settings:

Enable Foundation Assertions
    Debug    YES
    Release  NO

Disable Safety Checks  NO

What's wrong?


EDIT:

Sulthan's answer shows how to disable asserts globally for both debug and relase builds. That is not what I need. I want it to work as expected - asserts should be enabled in debug but disabled in release builds.

By default it works that way - and it also works that way in my main project. But it does not work for asserts located in Framework project that is linked from that main project (details in this question). Why? How to fix it?

Community
  • 1
  • 1
Rasto
  • 17,204
  • 47
  • 154
  • 245
  • Did you try my [answer](http://stackoverflow.com/a/24038197/669586)? – Sulthan Apr 02 '16 at 14:25
  • @Sulthan No I did not. I thought it should not be required to add any custom flags to ensure it ignores asserts in release. I will try it now. – Rasto Apr 02 '16 at 14:46
  • I also think it shouldn't be required (it was required in one of the first beta versions). – Sulthan Apr 02 '16 at 14:55
  • @Sulthan Please see my edit - the default behaviour of assers is broken in Frameworks. – Rasto Apr 03 '16 at 17:00

1 Answers1

2

The options you have tried:

Enable Foundation Assertions is in the preprocessing section (Macros). Swift is not preprocessed and does not use macros. This option disables NSAssert, NSParameterAssert and similar macros commonly used in Objective-C.

Disable Safety Checks is a performance option:

By default, the standard library guarantees memory safety. Many functions and methods document the requirements that must be satisfied by the caller, such as an array index being valid; memory safety is guaranteed even if a requirement is violated. However, violating a requirement can trigger a runtime error. APIs that include the word “unsafe” in their name let you explicitly disable safety checks in places where you need the additional performance. It’s your responsibility to verify the memory safety of code that uses unsafe APIs. Memory safety is also not guaranteed if there is a race condition in multithreaded code.

(Swift Library Reference)

You should probably try my answer here (use -assert-config Release in Other Swift Flags).

Or just keep the asserts in production builds. Every failing assert is a bug and in general it's better to know about a bug as soon as possible.

Community
  • 1
  • 1
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Where do I put these flags? Which project, which target? I know about the bugs there. They are rare, minor and I plan to fix them in future releases. Crashes cause by asserts are much more severe. – Rasto Apr 02 '16 at 14:50
  • @drasto You have to put them to the project & target that contains the asserts. It's just a compiler flag. – Sulthan Apr 02 '16 at 14:53
  • It does not seem to work - I put `-assert-config Debug` to project containing asserts (this is Framework project linked from another project), run it from XCode but the asserts are still hit – Rasto Apr 02 '16 at 15:01
  • @drasto You should be putting `Release` there, not `Debug`. – Sulthan Apr 02 '16 at 15:09
  • I wanted to try if it makes any difference without having to archive, publish and download - Debug should disable assert during debug as well, right? – Rasto Apr 02 '16 at 15:10
  • Oh sorry, I see. I think I understand now how that flag works. And it really disables asserts. – Rasto Apr 02 '16 at 15:17
  • Do you know of any way how to disable assert for debug builds **only**? If I set `-assert-config Release` to `Other Swift Flags` asserts are disabled for both debug and release. If I just set it `Other Swift Flags` to `-assert-config Release` for `Release` (after expanding `Other Swift Flags` with arrow) and leave it empty for `Debug` the asserts are not disabled in release builds... – Rasto Apr 03 '16 at 11:41