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 trialJordan Ward
2,395 PointsCan 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!
1 Answer
Mitch Little
11,870 PointsHi 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
2,395 PointsThanks Mitch, oh I realise now, just used the wrong syntax, I forgot the \ . Same to you mate!
Jordan Ward
2,395 PointsJordan Ward
2,395 PointsAlso just to clarify the values were set as constants earlier so thats not why I got the error haha.