Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have converted a String to an Int by by using toInt(). I then tried multiplying it by 0.01, but I get an error that says Could not find an overload for '*' that accepts the supplied argument. Here is my code:

var str: Int = 0
var pennyCount = 0.00

str = pennyTextField.text.toInt()!
pennyCount = str * 0.01

From reading other posts it seems that the answer has to do with the type. For example if the type is set as an Integer then it gets a similar error. I have tried changing the type to an Int, but that doesn't seem to solve the problem.

I have also tried setting the type for 'str' and 'pennyCount' as Floats and Doubles and all combinations of Floats, Doubles, and Ints. My guess is the the problem has to do with toInt() function's conversion of a String to an Integer.

Could someone help clarify what the issue may be?

like image 977
fairbanksdan Avatar asked Jun 05 '14 22:06

fairbanksdan


1 Answers

Swift seems to be fairly picky about implied type casting, so in your example you're multiplying str (an Integer) by 0.01 (a Double) so to resolve the error, you'll need to cast it like this:

var str: Int = 0
var pennyCount = 0.00
str = pennyTextField.text.toInt()!
pennyCount = Double(str) * 0.01
like image 130
johnnyclem Avatar answered Oct 25 '22 12:10

johnnyclem