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

Jordan Ward
Jordan Ward
2,395 Points

Can you combine Concatenation and Interpolation in the same statement?

Hey guys just a quick one! So I was messing around with interpolation and concatenation and when I tried to combine the two I got a compiler error. In the beginning I was just trying to add two interpolated statements in the one line like so...

Example: let otherRepresentationOfHouseParty = "(houseParty)" "(numberAdress)"

Then I thought surely you can just add the two together like so. Example: let otherRepresentationOfHouseParty = "it was a (houseParty) at" + "(numberAdress), located in (suburb)"

My question purely for the sake of knowing: can you combine two interpolated strings in the one statement? And also can you combine interpolated statements and concatenated statements in the one line? Tried to explain the situation best I could, any help would be greatly appreciated!

Jordan Ward
Jordan Ward
2,395 Points

Also just to clarify the values were set as constants earlier so thats not why I got the error haha.

1 Answer

Hi Jordan,

Yes, you can combine string interpolation and concatenation.

The reason you are receiving an error is because you haven't including a '\' before your interpolated constants.

The solution you are looking for looks something like this:

let otherRepresentationOfHousePArty = "it was a \(houseParty) at" + "\(numberAddress), located in \(suburb)"

All the best,

Mitch

Jordan Ward
Jordan Ward
2,395 Points

Thanks Mitch, oh I realise now, just used the wrong syntax, I forgot the \ . Same to you mate!