我想用一个乘法器乘以一个文本字段,但我一直得到下面的错误。有人能帮忙吗?使用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;

最佳答案

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

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

关于ios - 乘法文本字段错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33713777/

10-12 07:16