在Xcode中转换整数字符串

时间:2017-03-02 21:48:10

标签: swift

我在Xcode中编写了一些代码,错误在最后一行。有人可以帮我把字符串转换成整数吗?

@IBAction func btnCalc(_ sender: Any) {
    let numb1 = Int(txtN1.text!)
    let numb2 = Int(txtN2.text!)

    let calcResult = (numb1)! + (numb2)!
    labelResult.text = (calcResult)
}

1 个答案:

答案 0 :(得分:2)

INT(myString的)

Swift 1.x

let myString: String = "256" let myInt: Int? = myString.toInt()

Swift 2.x,3.x

let myString: String = "256" let myInt: Int? = Int(myString)