从不使用不可变值“textfieldint”的初始化;
考虑替换为对“u”的赋值或删除它,并且
文本字段2输入
我收到了两次关于textfieldint的警告
这就是我所有的代码:

class ViewController: UIViewController {

    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!
    @IBOutlet weak var output: UILabel!

    @IBAction func calculate(_ sender: AnyObject) {
        let textfieldInt: Int? = Int(textField1.text!)
        let textfield2Int: Int? = Int(textField2.text!)
        let convert = textField1.text! + textField2.text!
        let convertText = String(convert)
        output.text = convertText

}

最佳答案

您收到警告是因为,正如警告所说,您正在实例化textfieldInttextfield2Int,但您没有使用创建的整数textfieldInttextfield2Int计算为let convert,而是将字符串textField1.text!textField2.text!一起添加…
我想,你希望你的功能是:

@IBAction func calculate(_ sender: AnyObject) {
    let textfieldInt: Int? = Int(textField1.text!)
    let textfield2Int: Int? = Int(textField2.text!)
    let convert = textfieldInt + textfield2Int
    let convertText = String(convert)
    output.text = convertText
}

关于swift - 如何将textField.text值转换为Integer并将两个整数相加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40109695/

10-15 23:36