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 Manipulation

I'm pretty sure, that by the 2nd question, my answer is right, but it keeps saying it isn't correct.

Thanks.

strings.swift
// Enter your code below
let name = "Justus"
let greeting = "\("Hi there,") \(name)"
let finalGreeting = "\(greeting) \("How are you?")"

Thanks guys.

5 Answers

let name = "Justus"
let greeting = "Hi there, \(name)"
let finalGreeting = greeting +" How are you?"

This code works well

The greeting variable will actually cause an error to be thrown, as the compiler will recognize the quotation mark after the Hi there, statement as the end of the string. When the quote is removed, yes this will work as well. :]

In Swift, the use of \(someValue) within a string is for the purpose of converting some non-string type or some variable into the format of a string without the need of string concatenation (using the + operator). Therefore, what is being looked for is simply typing the string out and including the variable within the \()

let someString = "This is just a some string and a string interpolation via \(someVariable)"

I updated my answer to properly display the string interpolation as \()

But when my code is // Enter your code below let name = "Justus" let greeting = "("Hi there,) (name)" let finalGreeting = "(greeting) ("How are you?")" it still causes an error, what is wrong now? Thanks!

It looks like my comment did not include the backslash \ for some reason. My apologies. What I was meaning to say is like my example. The use of the () is meant for variables and non-string types. Therefore when you have a string such as "Hi there", it is just plain text, right? No variables, just words. This is meant to be stored in a variable or printed exactly as a string, without the need of the surrounding parenthesis.

The backslash and parenthesis are used in string interpolation which is when the compiler evaluates the given variable (which may be a string) or non-string type and turns it into a string.

Example:

let name = "Justus"
let phrase = "Here we will use your name, \(name)"

// phrase would be: "Here we will use your name, Justus"

So what you are looking to do is only put the variable in the () and the rest of the text is just written out.

You are looking to do something like this:

let someName = "Justus"
let someWords = "Hello there, \(someName)." // greeting
let finalOutput = "\(someWords) How are you?" // finalGreeting

Please let me know if this is more clear for you?

Thanks Joe!

When I use your code it still gives a bump :/

A bump?