@IBOutlet weak var textBox: UITextField!
@IBOutlet weak var result: UILabel!
@IBAction func calculate(sender: AnyObject) {
var age = textBox.text.toInt()
var Years = age! * 7
result.text = "your cat is "+ catYears + "cat years old"//cat years is an int who do i print it
}
所以在将textBox转换为整数后,如何以整数形式打印它。
答案 0 :(得分:17)
result.text = "your cat is \(Years) cat years old"
答案 1 :(得分:2)
有两种基本方法:
result.text = "Your cat is \(catYears) years old."
result.text = "Your cat is " + catYears.description + " years old."
您还可以使用NSNumberFormatter
,它支持更复杂的功能,例如千分之一分隔符等等:
let formatter = NSNumberFormatter()
formatter.hasThousandSeparators = true
let catYearsString = formatter.stringFromNumber(catYears)
result.text = "Your cat is " + catYearsString! + " years old."