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 trialrazmig pulurian
Courses Plus Student 23,889 PointsWhy did the string interpolation example use "\(222)" instead of "222" to combine the number and street?
The following string interpolation example was shown near the end of the video
let interpolatedStreetAddress = "(222) (street)"
Is there a reason the street number was placed inside of ()? Can't we just write the following?
let interpolatedStreetAddress = "222 (street)"
3 Answers
Jhoan Arango
14,575 PointsHello :
What Pasan was explaining in the video is that you cannot concatenate, a String, with an Int. You can only concatenate Strings. So in his example he is using string interpolation to show that you can combine two types, Int and Strings.
// Here we are using two types. An Int, and a String.
let interpolatedStreetAddress = "\(222) \(street)"
// Here we are using two strings, which is something you can do, depending on what you want to achieve.
let interpolatedStreetAddress = "222 \(street)"
Perhaps if Pasan had declared a constant, with an Int value then you would have gotten his point.
let numberOfStreet = 222
let interpolatedStreetAddress = "\(numberOfStreet) \(street)"
Good luck.
Josue Gisber
Courses Plus Student 9,332 Pointsyou can also use both method in same line. You can interpolate like pasan did or you can use: "(222)" + street. The only down side with this method is that you have to add a space using " " and that is an extra work. Just a thought. But of course the main point of this video is showing how to use interpolation with different types of data
Graeme Reed
Courses Plus Student 413 PointsI understand that these courses are trying to use simple points for teaching. I believe there is a power in interpolation, but I fail to see it illustrated here when it could've been "222" + street. Or street = "222 West Street" In this example it just seems entirely unnecessary. I feel like I'm missing something. It seems like this is basically illustrating that interpolation is a more efficient way to piece together long bits of information. Is that what I should be getting from this section?
Josue Gisber
Courses Plus Student 9,332 PointsWell if you do it the way you put it, then is not an Int but a String. He is interpolating two different types of data in one string
razmig pulurian
Courses Plus Student 23,889 Pointsrazmig pulurian
Courses Plus Student 23,889 PointsAh, I see your point. Subtle.
I did think to myself: "If he declared the constant earlier, then I can see why he used ()". I didn't get that he was trying to drive the point home.