【问题标题】:Swift presentViewController completion block working only in debug not called in releaseSwift presentViewController 完成块仅在调试中工作而不在发布中调用
【发布时间】:2015-03-16 08:09:52
【问题描述】:

我在调试和发布中的完成块有一个奇怪的行为。 例如:

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

在调试中: println("保存布尔值") 打印字符串 相对而言: println("save bolean") 不打印任何内容

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

种类 安德烈亚

【问题讨论】:

  • 是的,同样的问题!
  • 升级到 Xcode 6.3 解决了这个问题。

标签: swift closures completion-block


【解决方案1】:

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

在发布版本中,有时不会调用闭包,尤其是当它们按如下顺序排列时:

array.map({ $0 * 2 }).map({ $0 * 3 }).map({ $0 * 4 })...

该问题仅出现在 Swift 中,而不出现在 Objective-C 中。如果您的应用不需要通过优化获得更好的性能,可以通过将 Swift Compiler Optimization Level 设置为 None [-Onone] 来解决此问题。

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

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

sourceViewController.presentViewController(ccVC, animated: true, completion: completionHandler)

我对我的项目采用前一种解决方法,因为我想保持我的代码简单并且不需要通过优化来提高性能。

【讨论】:

  • 将近 2 年后你救了我!
【解决方案2】:

我在 xcode 7 中发现了同样的问题。 例如

  let delay = 0.2 * Double(NSEC_PER_SEC)
  let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

            dispatch_after(time, dispatch_get_main_queue(), {



            })

仅当我还设置了优化级别 None[-OO]

时才有效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多