加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:48:55 所属栏目:百科 来源:网络整理
导读:我在Debug和Release中有一个完成块的奇怪行为. 例如: sourceViewController.presentViewController(ccVC,animated: true,completion: { () - Void in NSUserDefaults.standardUserDefaults().setBool(true,forKey: kChromeCastInstructionsShown) NSUserDef
我在Debug和Release中有一个完成块的奇怪行为.
例如:

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

在调试中:
???println(“save bolean”)打印字符串
在重新:
?println(“save bolean”)什么都不打印

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


安德里亚

解决方法

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

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

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

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

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

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

sourceViewController.presentViewController(ccVC,completion: completionHandler)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读