3

Hi after migrating to swift3. I m having this issue? Do your guys know what wrong?

ambiguous reference to member "/"

let rateForPeriod = interestRate! / Double(100) / Double(K.monthsEachYear)
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Stefan Lam
  • 130
  • 10

2 Answers2

1

Congratulations! You may have just found a bug in the compiler!

Let's try a few variations of your code:

This fails:

let optional: Double? = 1
let result = optional! / Double(1) / Double(1)

This works:

let optional: Double? = 1
let unwrapped = optional!
let result = unwrapped / Double(1) / Double(1)

This fails:

let rateForPeriod = (Double(1) as Double?)! / Double(1) / Double(1)

But this works:

let rateForPeriod = ((Double(1) as Double?)! as Double) / Double(1) / Double(1)

So this works too:

let optional: Double? = 1
let rateForPeriod = optional! as Double / Double(1) / Double(1)

That seems to be a side effect (probably bug) of the new optional unwrapping rules.

Note that doing only one division at a time works as expected:

let optional: Double? = 1
let rateForYear = optional! / Double(1)
let rateForPeriod = rateForYear / Double(1)

What should we do?

  • File a bug report
  • Stop force unwrapping things

Try to see if you can make interestRate not be an optional, use a guard clause or give it a default value like this: let rate = interestRate ?? 0.

NiñoScript
  • 4,523
  • 2
  • 27
  • 33
0

If I were you, I would use optional binding to check whether interestRate is nil or not.

let interestRate: Double? = 0.4

if let interestRate = interestRate {
  let rateForPeriod = interestRate / Double(100) / Double(1)
}
Wilson
  • 9,006
  • 3
  • 42
  • 46