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 Solution: Thinking About Types

Why don't I have to use the \() to interpolate?

In the Thinking About Types excercise within the Manipulating Data practice sessions there is code similar to the following:

var isReady = true isReady = false

let value = 10 let valueTimesThree = value * 3

let name = "Pasan" let lastName = "Premaratne"

In the second section, I would assume that in order to use the value constant within the valueTimesThree constant I would have had to write it like this, 'backslash'(value). Why don't I need to do this?

1 Answer

andren
andren
28,538 Points

The \() syntax is used to insert a variable within a string, it makes it clear to Swift that you are referencing a variable and not just typing normal text. Outside of strings that distinction is not needed, as Swift assumes that all words typed is either a keyword, variable or something similar.

That means that whenever you want to reference a variable outside a string you simply have to type its name, nothing more. This is true regardless of whether the variable is used in a math operation, passed to a function or pretty much anything else.