在Int或Double中转换UITextField

时间:2016-10-11 19:34:55

标签: ios swift swift3

我遇到了这段代码的问题,快速学习,我无法理解为什么。

我想要做的是用户输入两个TextField两个数字,单击一个Button,然后在Label上获得结果。

我知道TextField不允许使用Int,但只允许使用字符串,我知道有将字符串转换为Int的方法,但它对我不起作用。

以下是代码:

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var firstNumber: UITextField!
    @IBOutlet weak var secondNumber: UITextField!
    @IBOutlet weak var resultButton: UIButton!
    @IBOutlet weak var resultNumber: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    var firstNumberConv :Int? = Int(firstNumber.text)
    var secondNumberConv :Int? = Int(secondNumber.text)
    var result = firstNumberConv + secondNumberConv

    @IBAction func resultFunction(_ sender: AnyObject) {
        resultNumber.text = result

    }

}

5 个答案:

答案 0 :(得分:2)

为什么不把你的代码放在你的行动中

类似这样的事情

@IBAction func resultFunction(_ sender: AnyObject) {
    let firstNumberConv :Int? = Int(firstNumber.text!)
    let secondNumberConv :Int? = Int(secondNumber.text!)
    let result = firstNumberConv! + secondNumberConv!
    resultNumber.text = String(result)

}

我希望这有助于你

答案 1 :(得分:2)

请尝试以下代码:

class ViewController: UIViewController {


    @IBOutlet weak var firstNumber: UITextField!
    @IBOutlet weak var secondNumber: UITextField!
    @IBOutlet weak var resultButton: UIButton!
    @IBOutlet weak var resultNumber: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func resultFunction(_ sender: AnyObject) {

        let firstNumberConv :Int? = Int(firstNumber.text!)
        let secondNumberConv :Int? = Int(secondNumber.text!)
        let result = firstNumberConv! + secondNumberConv!

        resultNumber.text =  String(result)

    }

}

答案 2 :(得分:1)

首先,必须将代码放入函数中。

其次,要保持一致!

您使用数字键盘然后去打开选项:

@IBAction func resultFunction(_ sender: AnyObject) {
   let firstNumberConv = Int(firstNumber.text)!
   let secondNumberConv = Int(secondNumber.text)!
   resultNumber.text = "\(firstNumberConv + secondNumberConv)"
}

或者您正在使用标准键盘,然后使用可选绑定来检查用户是否键入了非数字字符:

@IBAction func resultFunction(_ sender: AnyObject) {
   if let firstNumberConv = Int(firstNumber.text), 
      let secondNumberConv = Int(secondNumber.text) {
        resultNumber.text = "\(firstNumberConv + secondNumberConv)"
   }
}

答案 3 :(得分:0)

变量用法中的简单误解。 变量“result”在ViewController对象启动时估算,而不是在调用“resultFunction”时估算。

所以你可以按照以下方式更正

@IBAction func resultFunction(_ sender: AnyObject) {
    let firstNumberConv = Int(firstNumber.text) ?? 0
    let secondNumberConv = Int(secondNumber.text) ?? 0
    let result = firstNumberConv + secondNumberConv
    resultNumber.text = "\(result)"
}

您应该删除所有变量定义部分。我的意思是这部分

var firstNumberConv :Int? = Int(firstNumber.text)
var secondNumberConv :Int? = Int(secondNumber.text)
var result = firstNumberConv + secondNumberConv

希望它能为你效劳。 感谢

答案 4 :(得分:0)

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var firstNumber: UITextField!
@IBOutlet weak var secondNumber: UITextField!
@IBOutlet weak var resultButton: UIButton!
@IBOutlet weak var resultNumber: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
  }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

var firstNumberConv :Int? = Int(firstNumber.text)
var secondNumberConv :Int? = Int(secondNumber.text)
var result = firstNumberConv + secondNumberConv

@IBAction func resultFunction(_ sender: AnyObject) {
    resultNumber.text = result

    }

  }

将您的代码放入viewDidLoad()方法中,或者在下面执行此操作会更好

  func updateNumber(){

  let firstNumberConv = Int(firstNumber.text!)
  let secondNumberConv = Int(secondNumber.text!)
  let result = firstNumberConv! + secondNumberConv!
  resultNumber.text = String(result!)

  }

然后调用您的方法

@IBAction func resultFunction(_ sender: AnyObject) {
    updateNumber();
}

或者,如果您想转换为Double,只需更改

      Int(firstNumber.text!) to Double(firstNumber.text!)