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 Object-Oriented Swift Value vs Reference Types Final Exam - Solution

Multiply an Int with a Double

I was happy to pass this final exam on Object Oriented Swift but I pretended my constant "sides" was of type "String". I see the teacher didn't encounter any problem because his calculation in the getter is sideLength * sideLength (Double). But I wanted to try with sideLength * sides (Double * Int). Obviously I get an error because I can't multiply a Double by an Int. But what would be the workaround ?

2 Answers

Convert your Int to a Double using the Double() method. Example:

let a = 3.0
let b = 2
let c = a * Double(b)  // outputs 6.0
let d = Int(c)  // converts back your result to an Int if needed

Great! Thanks :-)

Pas de soucis ;-)