Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Int to String in Swift?

Here is my code:

var score = 0

@IBOutlet weak var ourLabel: UILabel!
@IBAction func ifscoreButton(sender: UIButton) {
    score++

    ourLabel.text = score
}

The problem is I can't change the score value to a string. Any ideas on how to do this? Any help would be greatly appreciated !

2 Answers

The fancy term for it is "String Interpolation", but all you really need to know is how to do it. You just make a String as usual by writing a pair of double quotes, but between those quotes, you write a backslash and a pair of parentheses. Between those parentheses, you write whatever you want converted to a String. In your case, it would look like this:

ourLabel.text = "\(score)"

Thanks for the response! I did what you said and it worked perfectly!

You can just use toString(score) to convert an int to a string.