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.