Swift presentViewController完成块仅在未在发布中调用的调试中工作

And*_*zza 4 closures swift completion-block

我在Debug和Release中有一个完成块的奇怪行为.例如:

        sourceViewController.presentViewController(ccVC, animated: true, completion: { () -> Void in
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: kChromeCastInstructionsShown)
            NSUserDefaults.standardUserDefaults().synchronize()
            println("save bolean")
        })
Run Code Online (Sandbox Code Playgroud)

在调试中:println("save bolean")print string在relase:println("save bolean")中不打印任何内容

对这种行为有什么想法吗?有人试验一个明确的解决方案?

亲切的安德里亚

Yoi*_*aya 10

它看起来像这里讨论的Swift编译器错误(至少版本1.1):https: //github.com/ReactiveCocoa/ReactiveCocoa/issues/1632

在发布版本中,有时不会调用闭包,特别是当它们按顺序排序时:

array.map({ $0 * 2 }).map({ $0 * 3 }).map({ $0 * 4 })...
Run Code Online (Sandbox Code Playgroud)

该问题仅出现在Swift中,而不是出现在Objective-C中.如果您的应用程序不需要通过优化获得更好的性能,则可以通过将Swift编译器优化级别设置为无[-Onone]来进行设计.

截图

或者另一种解决方法是将闭包命名为函数:

func completionHandler() {
    NSUserDefaults.standardUserDefaults().setBool(true, forKey: kChromeCastInstructionsShown)
    NSUserDefaults.standardUserDefaults().synchronize()
    println("save bolean")
}

sourceViewController.presentViewController(ccVC, animated: true, completion: completionHandler)
Run Code Online (Sandbox Code Playgroud)

我将以前的解决方法用于我的项目,因为我希望保持我的代码简单,并且不需要通过优化来提高性能.