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

Raymond Espinoza
Raymond Espinoza
1,989 Points

swift types

How do you get the firstValue = 2 and second value =4 to equal 8 and put it into string interpolation ?

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

Hi Raymond I'll like to give this a try...

In this question the keyword you are looking for is "interpolation". When you interpolate two strings. You add the strings together.

it should look something like this...
let firstValue = 2
let secondValue = 4

let fullValue =  firstValue * secondValue

Swift is so smart that with the constant name along is enough for Xcode to recognize its value! :) I hope it helps.

-roman

Raymond Espinoza
Raymond Espinoza
1,989 Points

i know that but how do i compute the product and print out the product in a formatted string?The string should read: "The product of 2 times 4 is 8".

1 Answer

Hi Raymond,

It is correct that you need to interpolate strings to complete this exercise. The word interpolate means to insert. Adding strings together is generally termed concatenation - other, simpler, words are available!

You have started by creating your two values, firstValue and secondValue. That's great. Not, you want to create a further constant called product which holds firstValue * secondValue. Got that? Something like:

let product = firstValue * secondValue

You then need to output all that in a string. You're given the string in the challenge: "The product of 2 times 4 is 8". Now we use interpolation! The 2, 4 and 8 in that string are all held in constants in our code. We can insert them by using a backslash, brackets and the variable name - just like in the videos. Assign that new string into another constant called output. The final code looks like:

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

Let me know if that doesn't work for you!

Steve.