【问题标题】:What is causing the block of Swift code to compile much slower than others?是什么导致 Swift 代码块的编译速度比其他代码慢得多?
【发布时间】:2016-10-17 04:50:06
【问题描述】:

我最近开始使用构建时间分析器分析我的 Swift 项目的编译时间:https://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode。我注意到这个方法编译起来特别慢:

func placeStatusSwitch() {
        let switchX = tableView.center.x - (statusSwitch.frame.width / 2)
        var switchY = tableView.contentInset.top - statusSwitch.frame.height + tableView.contentOffset.y

        if let refreshControl = refreshControl {
            let navigationBarHeight = navigationController?.navigationBar.frame.height ?? 0
            let expectedTopInset = UIApplication.shared.statusBarFrame.height + navigationBarHeight + statusSwitch.frame.height + MyTheme.verticalMargin - 4

            if tableView.contentInset.top >= expectedTopInset || refreshControl.isRefreshing {
                switchY = switchY - refreshControl.frame.height
            }
        }

        statusSwitch.frame = CGRect(x: switchX, y: switchY, width: statusSwitch.frame.width, height: statusSwitch.frame.height)
    } 

我尝试使用以下方式更改 Nil Coalescing Operator:

var navigationBarHeight: CGFloat = 0
            if let navBarHeight = navigationController?.navigationBar.frame.height {
                navigationBarHeight = navBarHeight
            }

但是编译时间仍然很慢。有什么想法可能导致减速吗?

【问题讨论】:

  • 对于较慢的执行,您必须熟悉 gcd.. 在此运行您希望在后台减慢的进程。并在执行期间使用当前进程对其进行管理。都是关于线程管理的。
  • Scientific Wild-Ass Guess (SWAG):会不会是在算术表达式的类型推断期间?如果你给出这些明确的类型会有帮助吗?
  • @ColinBarrett 你的意思是switchX 等添加CGFloat?
  • @KexAri 是的。或者,如果这不起作用,请添加中间绑定。完全猜测,就像我说的那样。

标签: ios swift xcode


【解决方案1】:

你也可以试试:

  1. 添加显式类型注释而不是推断类型。
  2. 将冗长的数学计算分解为较小的计算。

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 2013-08-08
    • 2021-09-09
    相关资源
    最近更新 更多