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
}

so after turning textBox into an integer how do i print it in integer form.

9
  • you can't down rate it off an assumption Jun 30, 2015 at 1:56
  • 2
    Just println(catYears). Or get fancy: println("Cat Years: \(catYears)")
    – zaph
    Jun 30, 2015 at 1:57
  • text = "your cat is \(catYears) old.". I recommend reading the swift book.
    – ahruss
    Jun 30, 2015 at 1:57
  • 1
    Re-evalate the word "can't", in "You can't down rate it off an assumption". Sure can but the down vote you are referring to I didn't. But it would help if you would read the question from the top and make sure it is consistent, complete and askig the correct question.
    – zaph
    Jun 30, 2015 at 2:00
  • 1
    Now I am surprised, I became convinced that I had answered the wrong question. Really.
    – zaph
    Jun 30, 2015 at 2:03

2 Answers 2

25
result.text = "your cat is \(Years) cat years old"
4

There are two basic ways:

result.text = "Your cat is \(catYears) years old."

result.text = "Your cat is " + catYears.description + " years old."

You can also use NSNumberFormatter, which supports more complex features like thousandths separators and so on:

let formatter = NSNumberFormatter()
formatter.hasThousandSeparators = true
let catYearsString = formatter.stringFromNumber(catYears)

result.text = "Your cat is " + catYearsString! + " years old."

Not the answer you're looking for? Browse other questions tagged or ask your own question.