表达方式过于复杂,无法在合理时间内解决;考虑将表达式分解为不同的子表达式
Posted
技术标签:
【中文标题】表达方式过于复杂,无法在合理时间内解决;考虑将表达式分解为不同的子表达式【英文标题】:Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions 【发布时间】:2015-09-12 20:34:49 【问题描述】:当我编译以下代码时:
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
我在另一个项目中尝试了代码,它编译没有问题。
我该如何解决这个问题?
【问题讨论】:
【参考方案1】:尝试像这样分开你的计算:
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)
【讨论】:
@confile 注意更容易理解、验证正确性和调试!这些都是好代码的属性。以上是关于表达方式过于复杂,无法在合理时间内解决;考虑将表达式分解为不同的子表达式的主要内容,如果未能解决你的问题,请参考以下文章
添加 4 个滑块值时,表达式太复杂,无法在合理的时间错误内解决
编译器无法在 SwiftUI 中的合理时间内对该表达式进行类型检查?
TS 表达式生成的联合类型过于复杂,无法用 Material-UI 和 @react-three/fiber 表示