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 (retired) Types Strings

String 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Ok 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!

makes sense.. using this instead of tokens like %d %i etc etc ...

Thanks !

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

James Richardson Exactly! Except that now you don't have to know what data type it is :smiley: Whatever that value is for that variable will be inserted there. That's also true for lists and arrays if you give the index.