Swift - 如何从UITextField中乘以值

时间:2014-07-08 06:37:59

标签: uitextfield swift

@IBOutlet weak var labelLabel: UILabel
@IBOutlet weak var textFieldField: UITextField
@IBAction func buttonButton(sender: AnyObject) {

   var conversionRate = 7

        self.labelLabel.text = self.textFieldField.text // Here I'm trying to multiply textFieldField by 7 (conversionRate)

        textFieldField.resignFirstResponder()
    }

您好,我想将UITextField中的值乘以7.有人可以帮我吗? 我尝试了一切。 .toInt(),intValue(),没什么

3 个答案:

答案 0 :(得分:1)

它应该与.toInt()

一起使用
if let number = self.textFieldField.text?.toInt() {
  self.labelLabel.text = "\(number * conversionRate)"
}

答案 1 :(得分:0)

你可以试试这个

    var conversionRate = 7;
    var value = self.nameLbl.text.toInt();
    value = value!*conversionRate;
    self.nameLbl.text = NSString(format:"%d",value!)as String;

    var conversionRate = 7;
    var value : Int = self.nameLbl.text.toInt()!;
    value = value*conversionRate;
    self.nameLbl.text = String(value);

OR

    var conversionRate = 7;
    if let number = nameLbl.text?.toInt() {
        self.nameLbl.text = "\(number * conversionRate)"
    }

答案 2 :(得分:0)

    let a = textFieldField.text
    let conversionRate = 7
    let b = Int(a!)! * conversionRate //use Double(a!)! instead of Int(a!)! if your are using a floating point value for "conversionRate" variable.
    labelLabel.text = ("\(b)")

快乐的编码!