Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.