找不到接受提供的参数的'*'的重载 [英] Could not find an overload for '*' that accepts the supplied argument

查看:154
本文介绍了找不到接受提供的参数的'*'的重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用 toInt()将 String 转换为 Int / code>。然后我尝试将它乘以0.01,但是我得到一个错误,说找不到接受提供的参数的'*'的重载。这是我的代码: p>

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

从阅读其他帖子看来,答案必须做与类型。例如,如果类型被设置为整数,那么它会得到类似的错误。我已经尝试将类型更改为Int,但这似乎不能解决问题。

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.

我还尝试将str和pennyCount的类型设置为 Floats 双打 Floats 双打整数。我的猜测是这个问题与 toInt()函数的一个 String 转换为整数

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?

推荐答案

Swift似乎对隐含的类型转换是相当挑剔的,所以在你的例子中,你将str(一个整数)乘以0.01(一个Double),以便解决错误,你需要转换为:

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

这篇关于找不到接受提供的参数的'*'的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆