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 Basics Swift Types String Manipulation

Nan Wang
Nan Wang
96 Points

I don’t understand the difference between concatenation and interpolation.

For this one, why do I need to create a new constant “hi” in order to do the interpolation? And how can I próceres with concatenation?

// Enter your code below
let name = "Wang Nan"
let hi = "Hi there"
let finalGreeting = "How are you?"
let greeting = "\(hi), \(name)"
let Final = "\(greeting), \(finalGreeting)"

1 Answer

Justin Horner
Justin Horner
Treehouse Guest Teacher

Hello Nan Wang,

First, let me address the difference between concatenation and interpolation. While both can both be used to build strings, concatenation combines only string variables together. This means every non-string variable you want to include in a concatenated string must be converted to a string first.

With interpolation, you can provide variables of any type and Swift will do the work necessary to return a string with the variables represented as strings. In this specific example, it's not as clear that this is a benefit since the variables are all strings and could just as easily be concatenated.

Check out the answer to this post by Pasan. He goes into more detail and shows a code to help demonstrate.

I hope this helps.