**tl;dr**: If your Swift code always gets nil from the keychain in Release builds, try to set Swift Compiler - Optimization Level to `-Onone`. Took me a day to find this solution.

Recently I published an app with a share extension which needed access the keychain in the host app and the share extension (like in [this demo](http://dasdev.de/2014/08/12/ios8-share-extension-with-a-shared-keychain/)).

Everything worked great until I submitted to the App Store. The tester at Apple hat a crash right at the start of the app. To investigate the crash I took the archive and resigned it for Ad Hoc distribution to put it onto my iPhone. And yes, the app crashed right at the start.

To make sure I tested again the debug build and everything works great again.

One of the main differences between App Store builds and Debug builds is the build configuration. The App Store build uses Release builds. So I changed the debug scheme to use the Release configuration. The app crashed right at the beginning.

I searched the internet for similar experiences and solutions. I found similar problems but no solution. A lot of questions about this topic were about problems during the beta phase of Swift. I tried to pin down the problem to the line it was happening (all without the ability to break in the code because it only happened in Release builds).

I finally found out that items I got from the keychain always were nil. I searched again and in the end the second comment on [this answer](http://stackoverflow.com/a/24197966/498796) was the solution. I switched the Swift Compiler - Optimization Level to `-Onone` and it worked.