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

Swift: String Manipulation (Basics)

The following code will complete the second challenge for String Manipulation, but I found a workaround for adding an Integer to a String Literal via the Concatenation method:

let name = "Steven."
let greeting = "Hi there, \(name)"
let finalGreeting = (greeting) + " How are you?"

If changing "name" to an integer the "finalGreeting" Concatenation will include the Integer, believing it to be a String Literal!

let name = 222
let greeting = "Hi there, \(name)"
let finalGreeting = (greeting) + " How are you?"

That is expected behavior and actually why String interpolation is such a handy thing: The values wrapped in \() will be converted to a String from any type, it does not have to be a String. From the Apple docs example:

let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message is "3 times 2.5 is 7.5"

In the example above, the value of multiplier is inserted into a string literal as (multiplier). This placeholder is replaced with the actual value of multiplier when the string interpolation is evaluated to create an actual string.