2015-06-30 81 views
-2
@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轉換爲整數後如何以整數形式打印它。如何在Swift中打印整數

+0

什麼!沒有它不是 –

+0

你不能降低它的假設 –

+1

只是'println(catYears)'。或者看看:'println(「Cat Years:\(catYears)」)' – zaph

回答

12
result.text = "your cat is \(Years) cat years old" 
1

有兩種基本方式:

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."