Eric Eric - 2 months ago 12
Swift Question

Cannot convert value of type ‘Int’ to expected argument type ‘Double’

I cannot figure out how to convert value of type

Int
to expected argument type
Double
for
mph = kph * 0.6214
in the following code:

var mph : Double!
var kph : Float!

print("KPH MPH")

print("------------")

for kph in 60.stride(to: 130, by: 10) {
mph = kph * 0.6214
print("\(kph) \(mph)")
}


How do I program the
for
to produce the desired results?

Answer

Simply declare the initial value as literal Double (adding .0)

for kph in 60.0.stride(to: 130, by: 10) {

Then the compiler infers the type of the function as Double and converts the other literals implicitly.