-3

Can't get Xcode to compile, getting this error:

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

 self.locationButton.frame = CGRect(x: self.view.bounds.size.width/2 + buttonSize*1.4 - buttonSize/20 - buttonSize/2 , y: height/2 + (cardHeight+11)/2 - buttonSize/2, width: buttonSize/2, height: buttonSize/2)
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • 6
    “consider breaking up the expression into distinct sub-expressions” so do that. What’s the question? – matt Aug 15 '18 at 22:33

2 Answers2

3

There is a huge number of possible type and function choices here, only one of which is correct. The Swift compiler is not powerful enough today to decide quickly whether you want self.view.bounds.size.width/2 to treat 2 as an Integer, a Float, a CGFloat, or a Double. It doesn't know quickly if you want to use the Integer, CGFloat, or Double version of CGRect.init. It doesn't know quickly which of numerous versions of + you meant here. Some day it will get better tat that; today it takes too long to chase down the combinatorially explosive possibilities. So it asks you to simplify so that the evaluating this expression doesn't take several minutes (or longer).

As a possible simplification:

let x: CGFloat = self.view.bounds.size.width/2 + buttonSize*1.4 - buttonSize/20 - buttonSize/2
let y: CGFloat = height/2 + (cardHeight+11)/2 - buttonSize/2
let width: CGFloat = buttonSize/2
let height: CGFloat = buttonSize/2
self.locationButton.frame = CGRect(x: x, y: y, width: width, height: height)

That x calculation may still be too complicated, and you may want to break it up.

Many other languages avoid this problem by just promoting everything to Double which often works fine, but sometimes introduces subtle errors that are very hard to debug (for example, there are many Int values that cannot be expressed as a Double and you can enter a bizarre math world where x + 1 == x).

Swift chose to be explicit and careful with numeric conversions. That has some benefits. This is one of the trade-offs, however.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

Yay, the compiler say your expression is very large to process, part your expression like this:

let locationButton = UIButton()
let xSize = CGFloat(100/33 + 43/22)
let ySize = CGFloat(2030/2 + 30/22)
locationButton.frame = CGRect(x: xSize, y: ySize, width: 200/2, height: 200/2)

the numbers inside CGFloat just are reference tu part your expression, feel free to use your own values and try to analize the answer of the compiler. Regards!