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 trialJames R
Courses Plus Student 8,446 PointsString Interpolation ?
Is String Interpolation the sane as using #DEFINE in C ? Can someone please give me a real world example on why using ("stringName") is useful in Swift ?
1 Answer
Jennifer Nordell
Treehouse TeacherOk you're familiar with old school C I take it. So remember the old printf statements before cin and cout in C++? String interpolation is a little like this.
printf("2 * 3 is %d", 2*3);
So it will print out "2 * 3 is 6". That's string interpolation at its core. Take the same example in Swift.
let x = 2
let y = 3
let result = x*y
print("\(x) * \(y) is \(result)"
This works a bit like the %d etc that you're used to in C except that it inserts the value of that variable in that place. Hope that's helpful!
James R
Courses Plus Student 8,446 PointsJames R
Courses Plus Student 8,446 Pointsmakes sense.. using this instead of tokens like %d %i etc etc ...
Thanks !
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherJames Richardson Exactly! Except that now you don't have to know what data type it is Whatever that value is for that variable will be inserted there. That's also true for lists and arrays if you give the index.