用SWIFT将字符串转换为INT该应用程序基本上是通过输入初始和最终的速度和时间来计算加速度,然后使用一个公式来计算加速度。但是,由于文本框中的值是字符串,所以我无法将它们转换为整数。@IBOutlet var txtBox1 : UITextField@IBOutlet var txtBox2 : UITextField@IBOutlet var txtBox3 :
UITextField@IBOutlet var lblAnswer : UILabel@IBAction func btn1(sender : AnyObject) {
let answer1 = "The acceleration is"
var answer2 = txtBox1
var answer3 = txtBox2
var answer4 = txtBox3
3 回答
沧海一幻觉
TA贡献1824条经验 获得超5个赞
// toInt returns optional that's why we used a:Int? let a:Int? = firstText.text.toInt() // firstText is UITextField let b:Int? = secondText.text.toInt() // secondText is UITextField // check a and b before unwrapping using ! if a && b { var ans = a! + b! answerLabel.text = "Answer is \(ans)" // answerLabel ie UILabel } else { answerLabel.text = "Input values are not numeric" }
...let a:Int? = Int(firstText.text) // firstText is UITextFieldlet b:Int? = Int(secondText.text) // secondText is UITextField...
白板的微信
TA贡献1883条经验 获得超3个赞
更新SURVER 2.0的答案 :
toInt()
.toInt()
let a:Int? = Int(firstText.text) // firstText is UITextField let b:Int? = Int(secondText.text) // secondText is UITextField
慕容708150
TA贡献1831条经验 获得超4个赞
myString.toInt()
SWIFT 3.x
let myInt = Int(textField.text)
let myString = "556"let myInt = (myString as NSString).integerValue
- 3 回答
- 0 关注
- 3976 浏览
添加回答
举报
0/150
提交
取消