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 Swift Types Recap: Swift Types

Albert G
Albert G
4,142 Points

Stuck on coding challenge

not sure whats wrong with my code for step two

types.swift
// Enter your code below
let firstValue = 2
let secondValue = 4

let product = firstValue * secondValue

let output = " \("the product of") \(firstValue) \("times") \(secondValue) \("is")\(product)"
Albert G
Albert G
4,142 Points

ok so i got it to pass with let output = "The product of \(firstValue) times \(secondValue) is \(product)" but that code does not work in xcode

2 Answers

You can't use \() around plain text inside of a string.

Swift will try to evaluate the text, which will cause an syntax error. You should omit the () so that Swift understands you aren't trying to evaluate that piece of code but you want it to be part of the string.

Example:

Incorrect code:

let greeting = "\("Hello There"), \(name)"

Correct code:

let greeting = "Hello There, \(name)

I hope this hint will help you. :smile:

If you continue to struggle, jutst ask below and I'll give you additional hints :)

Good luck! ~Alex

Albert G
Albert G
4,142 Points

ok so i got it to pass with let output = "The product of \(firstValue) times \(secondValue) is \(product)" but that code does not work in xcode

Albert G
Albert G
4,142 Points

figured it out thanks

No problem :)

let firstValue = 2
let secondValue = 4

let product = firstValue * secondValue

let output = "The product of \(firstValue) times \(secondValue) is \(product)"

You didn't format the string interpolation correctly. We never created any constants named "the product of", or "times". The constants we have decried are "firstValue", "secondValue", and "output". These are the names we will use to represent an integer number within the sentence the challenge asks us to make based upon what the values of the the constants firstValue and secondValue are, in this case it is 2 and 4, respectively. The string assigned to the constant output should read "The product of 2 times 4 is 8". Within this string, the values 2, 4, and, 8 will be replaced with the names we assigned them to in between a backslash and pair of parentheses. "(firstValue)" therefore is really "2", with the name of the constant assigned to it "interpolated" (synonymn for inserted or replaced) .