Given the following function:
func greatestCommonDenominator(first: Int, second: Int) -> Int {
return second == 0 ? first : greatestCommonDenominator(second, first % second)
}
And a struct with the following stuff in it:
struct Fraction {
var numerator: Int
var denominator: Int
func reduce() {
let gcd = greatestCommonDenominator(numerator,denominator)
self.numerator /= gcd
self.denominator /= gcd
}
// stuff
}
I'm getting the following error:
error: 'Int' is not convertible to '@lvalue Float'
self.numerator /= gcd
^
error: 'Int' is not convertible to '@lvalue Float'
self.denominator /= gcd
^
'@lvalue Float'
?!?!? What? I don't have a Float anywhere in here. And the documentation seems to suggest that the /=
should return an Int
as I'm dividing two Int
s. How do I fix this?
ADDENDUM: I came across this problem working within a struct, however the problem seems reproducible anywhere.
let a = 10
a /= 5
This will produce the same problem. Even if we explicitly type a
as an Int
:
let a: Int = 10
a /= 5
The same problem remains. Swift seems to think the result of the /=
operator between two Ints is a Float.
EDIT: The problem with the addendum isn't actually that a /= 5
doesn't work. It does actually!
var a: Int = 4
var b: Int = 3
a /= b
Now a
is 3. The problem in the addendum was similar to the struct. In the addendum, a
was declared as a let
rather than a var
, and as such it is unassignable.