2015-11-14 79 views
1

我想乘以一個乘數的文本字段,但我不斷收到下面的錯誤。誰能幫忙?使用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; 
+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; 
0

您必須轉換Number1Double,操作數必須是同一類型。

var Number1 = Double(weight.text!)