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 2.0 Basics Swift Types String Interpolation

Admon Dallo
Admon Dallo
4,709 Points

Why does "\() \()" not always work?

The question wont show my placement of "forward slash", so where I put a FS means I would have put a forward slash in the code.

In the video, we were shown the following:

let interpolatedStreetAddress = "FS(222) FS(street)" - RESULTS: 222 West Street

BUT

when I try this:

let name = "John" let greeting = "FS(Hi there), FS(name)."

I get an error...

!'greeting used within its own type !Use of unresolved identifier 'Hi' Use of unresolved identifier 'there' Could not infer type for 'greeting'

I know the correct way to do this now is:

let greeting = "Hi there, FS(name)."

2 Answers

David Lin
David Lin
35,864 Points

The reason why

let printNumber = "\(123)" 

works is because the compiler recognizes 123 as a number literal.

For the same reason,

let printNumberString = "\("123")" 

will also work, because it is a string literal.

However,

let thisWillNOTWork = "\(123 456)" 

will NOT work, because the compiler doesn't know what to do with that space inbetween the numbers. 123 is a number, and 456 is a number, but that space is not. So, the compiler will complain.

Likewise, if you just use words which are not first defined as variables inside the parenthesis, the compiler won't know what they mean, and then you'll get an error.

That is, by itself,

let myWord = "\(word)"

will NOT work.

But if you first define it to be a variable,

let word = "word"
let myWord = "\(word)"

will work OK.

Admon Dallo
Admon Dallo
4,709 Points

Ohhh, I understand! Because numbers are constants and they can only mean their own number, unlike words can have different strings attached to them!

Thank you!!!

David Lin
David Lin
35,864 Points

You're welcome Admon, Glad it all makes sense now. ;-)

David Lin
David Lin
35,864 Points

That's because what you put inside () needs to be a printable constant or variable.
If you just a bunch of words like Hi there, the compiler will complain because it's expecting a constant or variable.

let hiThere = "Hi there"
let name = "Admon"
let greeting = "\(hiThere), \(name)"
// greeting is "Hi there, Admon"
Admon Dallo
Admon Dallo
4,709 Points

Thank you for the reply, David, but in the example, the 222 was never declared as var or let, it was just added.

let country = "United States of America"

let state = "California"

let city = "San Diego"

let street = "Main St."

let aptNumber = "7777"

let concatenatedAddress = country + ", " + state + ", " + city

//: String Interpolation

let interpolatedAddress = "FS(city), FS(state), FS(country)"

let interpolatedStreetAddress = "FS(2480) FS(street) FS(aptNumber) FS(city), FS(state), FS(country)"

RESULTS: "123 Main St. 7777 San Diego, California, United States of America"

No error.