乘以文本字段错误?

时间:2015-11-14 21:47:42

标签: ios iphone xcode swift calculator

我想将文本字段乘以乘数,但我不断得到下面的错误。有人可以帮忙吗?使用Swift。

Binary operator '*' cannot be applied to operands of type 'Int?' and 'Double'


    var Number1 = Int(weight.text!)
    let lidocainemult = (1.5)

 var lidoresult = Number1 * lidocainemult


    lidocaine.text = NSString(format:"%d",lidoresult)as String;

2 个答案:

答案 0 :(得分:0)

您必须先将变量转换为相同的类型。这里Double最有意义,因为不会丢失信息(与舍入生成Int不同!)。

var Number1 = Double(weight.text!)
let lidocainemult = (1.5)
var lidoresult = Number1 * lidocainemult
lidocaine.text = NSString(format:"%d",lidoresult)as String;

答案 1 :(得分:0)

您必须将Number1转换为Double,操作数必须属于同一类型。

var Number1 = Double(weight.text!)