Sam Johnson
Sam Johnson

Reputation: 13

Could not find an overload for '*' that accepts the supplied arguments

Started learning Swift earlier this week and I'm doing some quick practices to learn it.

I'm trying to convert Celcius into Farenheit, this is what I have:

    var tempInCelcius = 30

    var tempInFarenheit = tempInCelcius * 1.8 + 32

However I get the below error:

error: could not find an overload for '*' that accepts the supplied arguments

Am I missing something really obvious here?

Upvotes: 1

Views: 65

Answers (1)

vadian
vadian

Reputation: 285290

tempInCelcius is declared as Int (the default type for integer literals).
1.8 is inferred as Double.

In Swift you cannot do math with different types.

Solution is to declare tempInCelcius explicitly

var tempInCelcius : Double = 30

or implicitly

var tempInCelcius = 30.0

as Double. Then the multiplication works.

var tempInFahrenheit = tempInCelcius * 1.8 + 32

Unlike variables literal numbers like 32 have to distinct type and are inferred to the proper type of the operation (if possible)

Upvotes: 1

Related Questions