表达式过于复杂,无法在合理的时间内解决;考虑将表达式分解为不同的子表达式

Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

当我编译以下代码时:

  override func layoutSubviews() {
    super.layoutSubviews()

    let selectedBackgroundWidth = bounds.width / 2.0 - selectedBackgroundInset * 2.0
    selectedBackgroundView.frame = CGRect(x: selectedBackgroundInset + CGFloat(selectedIndex) * (selectedBackgroundWidth + selectedBackgroundInset * 2.0), y: selectedBackgroundInset, width: selectedBackgroundWidth, height: bounds.height - selectedBackgroundInset * 2.0)

    (titleLabelsContentView.frame, selectedTitleLabelsContentView.frame) = (bounds, bounds)

    let titleLabelMaxWidth = bounds.width / 2.0 - selectedBackgroundInset * 2.0
    let titleLabelMaxHeight = bounds.height - selectedBackgroundInset * 2.0

    let leftTitleLabelSize = leftTitleLabel.sizeThatFits(CGSize(width: titleLabelMaxWidth, height: titleLabelMaxHeight))
    let leftTitleLabelOrigin = CGPoint(x: floor((bounds.width / 2.0 - leftTitleLabelSize.width) / 2.0), y: floor((bounds.height - leftTitleLabelSize.height) / 2.0))
    let leftTitleLabelFrame = CGRect(origin: leftTitleLabelOrigin, size: leftTitleLabelSize)
    (leftTitleLabel.frame, selectedLeftTitleLabel.frame) = (leftTitleLabelFrame, leftTitleLabelFrame)

    let rightTitleLabelSize = rightTitleLabel.sizeThatFits(CGSize(width: titleLabelMaxWidth, height: titleLabelMaxHeight))
    let rightTitleLabelOrigin = CGPoint(x: floor(bounds.size.width / 2.0 + (bounds.width / 2.0 - rightTitleLabelSize.width) / 2.0), y: floor((bounds.height - rightTitleLabelSize.height) / 2.0))
    let rightTitleLabelFrame = CGRect(origin: rightTitleLabelOrigin, size: rightTitleLabelSize)
    (rightTitleLabel.frame, selectedRightTitleLabel.frame) = (rightTitleLabelFrame, rightTitleLabelFrame)
  }

我收到以下错误:

Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

我在另一个项目中尝试了代码,它编译没有问题。

如何解决这个问题?

尝试像这样分开计算:

let rightTitleLabelSize = rightTitleLabel.sizeThatFits(CGSize(width: titleLabelMaxWidth, height: titleLabelMaxHeight))
let x = floor(bounds.size.width / 2.0 + (bounds.width / 2.0 - rightTitleLabelSize.width) / 2.0)
let y = floor((bounds.height - rightTitleLabelSize.height) / 2.0)
let rightTitleLabelOrigin = CGPoint(x: x, y: y)
let rightTitleLabelFrame = CGRect(origin: rightTitleLabelOrigin, size: rightTitleLabelSize)
(rightTitleLabel.frame, selectedRightTitleLabel.frame) = (rightTitleLabelFrame, rightTitleLabelFrame)