iOS Swift Basics Swift Types Recap: Swift Types

Unsure what step2 is asking

Step2 in this exercise is very vague. Asking for some support in what the output is looking for.

// Enter your code below
let firstValue = 12
let secondValue = 15
let product = \(firstValue) * \(secondValue)
let output = \(firstValue) * \(secondValue) = \(product)

2 Answers

iOS Development with Swift Techdegree Student 285 Points

Hi Manos,

I believe Step 2 is asking you to write out a String statement such as:

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

Keep up the good work :)

Alex Koumparos
Alex Koumparos
Python Development Techdegree Student 36,858 Points

Hi Manos,

You have some invalid syntax in this line:

let product = \(firstValue) * \(secondValue)

The construct \(someVariableName) is only used inside strings and is used to tell Swift to evaluate the the code inside the parentheses and then turn the result of that evaluation into a string inside the rest of the string. This is called "string interpolation".

Outside of strings, you can just use variables normally. So, for example, if you wanted to add two numbers, firstNumber and secondNumber together, you could do this:

let sum = firstNumber + secondNumber

Hopefully this will get your product line working.

Next, we have the output line. This is where you do need to use string interpolation. Let's start by looking at the question to see what it wants the string to look like:

The string should read: "The product of 2 times 4 is 8".

Therefore, you need to end up with a string that has exactly that structure. Let's go back to the previous example to see what that might look like. Suppose we wanted to make a string that expressed in a sentence the sum of numbers firstNumber and secondNumber (and we'll assume we have the sum constant from the previous example), we could write it like this:

let text = "The sum of \(firstNumber) and \(secondNumber) is \(sum)"

Look closely at the structure, we have a single string (denoted by the double quote marks). Inside that string, any values we want to evaluate are using the string interpolation syntax (denoted by the \( ... )).

Hope that clears everything up for you.

Happy coding,