Drakalex
Drakalex

Reputation: 1538

Ambiguous reference in a simple calculation

I'm working with Xcode and Swift 3.

I have the following line:

let x = playableRect.minX + (hexagon.size.width / 2) + (i % 4) * hexagon.size.width

which returns the error Ambiguous reference to member '/'

I don't really know why it fails, when

let x = playableRect.minX + (hexagon.size.width * 2) + (i % 4) * hexagon.size.width

works, and

let x = playableRect.minX + (hexagon.size.width / 2) + i * hexagon.size.width

works too. The error occurs when I combine / and % in the same calculation. Do you have any idea why ?

Upvotes: 0

Views: 119

Answers (1)

Craig Siemens
Craig Siemens

Reputation: 13296

Since i is an Int, and everything else is a CGFloat it doesn't know how to handle those different types. You'll need to change it so everything is a CGFloat.

let x = playableRect.minX + (hexagon.size.width / 2) + CGFloat(i % 4) * hexagon.size.width

On a side note, the error message you got is weird. There must be something else going on in your code thats causing it to give you a kinda useless error message. I put the same code into a playground and got the following error which is way more helpful.

error: binary operator '*' cannot be applied to operands of type 'Int' and 'CGFloat'
x + (w / 2) + (i % 4) * w
              ~~~~~~~ ^ ~

Upvotes: 1

Related Questions