Swift在调试和发布模式下的行为不同 [英] Swift behaves differently on debug and release mode

查看:242
本文介绍了Swift在调试和发布模式下的行为不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这是否是Swift,XCode或Alamofire的问题,但我发现在混合的Swift / Objc应用程序中的不同地方有奇怪的行为。它只发生在Swift编写的部分,并使用闭包/网络。这里是一个示例代码,它发生:

  Alamofire.request(.DELETE,http://someUrl.com/user ,参数:nil)

中的.response {(请求,响应,数据,错误)//一些清除代码和警报
}



当我在iPhone上以调试模式运行应用程序时,一切正常清除代码和警报,当我执行删除帐户操作,运行上面的代码时,他们应该提交。



但是当我通过Testflight发送我的应用程序或直接在同一iPhone上运行我的应用,但使用发布版本配置,然后清除代码不会运行强>。看起来整个闭包没有被调用。



有没有人有这样奇怪的行为的经验,知道如何防止他们?我不知道这里的问题是什么,因此我很难找出一个解决方案,适用于调试和发布模式。



感谢您的帮助!



环境:
我使用Alamofire 1.1.3作为一个嵌入式框架集成到我的项目作为git子模块。该应用程序仅在iOS 8+上运行,我的iPhone 6上安装了iOS 8.1.2。

解决方案

一些类似的问题,当在类似于你的项目(Swift + Objective-C),在我的情况下,它是跳过一个循环中的一段代码,但它不是跳过循环本身的项目。
为了解决这个问题,我们在 Build Settings - > Apple LLVM 6.0代码生成 - >优化级别将Debug的值设置为最快,最小[-Os] ,这与Release相同,然后我们在Debug 。然后我们尝试改变两个 None [-O0] (这是Debug默认值),但它只是使得错误消失在调试模式而不是发布,所以我们不得不解决方法代码和更改一点UI。



我想相信这是编译器在发布,我们不能改变,也许它是一个Xcode Bug。


Not sure if that's an issue with Swift, XCode or Alamofire but I recognized strange behavior on different places within my mixed Swift/Objc app. It only happens in parts which are written in Swift and use closures/networking. Here's an example code where it happens:

Alamofire.request(.DELETE, "http://someUrl.com/user", parameters: nil)
     .response { (request, response, data, error) in
                 // some cleanup code and an alert
               }

When I run my app in Debug mode on my iPhone then it all just works, the cleanup code and the alert get presented like they should when I do the "delete account" action which runs the code above.

But when I send my app to users via Testflight or run my app on the same iPhone directly but using the Release build configuration then the cleanup code doesn't run and the alert doesn't show up. It looks like the whole closure doesn't get called.

Does anyone have experiences with such strange behaviors and knows how to prevent them? I'm not sure what's the issue here, therefore it is hard for me to figure out a solution that works on both Debug and Release modes.

Thank you for any help!

The environment: I'm using Alamofire 1.1.3 as an embedded framework integrated to my project as a git submodule. The app runs on iOS 8+ only, I have iOS 8.1.2 installed on my iPhone 6.

解决方案

I've had some similar issues when building for release in a project similar to yours(Swift + Objective-C), in my case it was skipping a piece of code within a loop, but it was not skipping the loop itself. In order to solve this we changed at the Build Settings -> Apple LLVM 6.0 Code Generation -> Optimization Level the value of Debug to Fastest, Smallest [-Os], which was the same of Release and then we got the same bug on Debug. Then we tried to changing both to None [-O0] (Which is Debug default value) but it only made the bug disappear on Debug mode not in Release, so we had to workaround that code and change a little bit of the UI.

I would like to believe that it's something the compiler does in Release that can't be changed by us, so maybe it's an Xcode Bug.

这篇关于Swift在调试和发布模式下的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆