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

Error Code for areaInMeters and/or Strange Display

1.) Received an error code for my areaInMeters

let height = 12 let width = 10 let area = height * width let areaInMeters = area / 10.764

2.) After reviewing the forum, changed my code to: let height = 12 let width = 10 let area = height * width let areaInMeters = Double(area)/ 10.764

but now my display is reading 11.1482720178372 instead of 12

I imagine there is a setting somewhere that is off - does anyone know what/where that would be?

3 Answers

Hi Amarie,

This isn't an error. 12*10/10.764 is not 12. Indeed, it's 11.1482720178372. If you want convert it to an integer, it would be 11. Here's how you would do that:

let height = 12
let width = 10
let area = height * width
let areaInMeters = Int(Double(area) / 10.764)

In that last line, work your way from the inside out: First you convert area to a double so you can divide it by a double, then you convert the result to an integer (11).

Make sense?

Props for checking the forum and fixing the division problem, by the way! Knowing how to search for and find answers will serve you well in your coding.

-Greg

Wow - post coffee it seems so obvious. LOL! THANK YOU.

Although... wait... why does HIS display say 12? When I did your suggestion, my answer rounded down (naturally). Why did his round up?

Should I get another coffee before asking this? It's going to be obvious again, isn't it... ? :)

Haha who is "He?" I'm not sure what lesson you're working on.

Edit: OK, I found the video you were watching. Amit Bijlani is using an early version of Swift in the video. Swift no longer lets you write the code he wrote! Swift will not implicitly convert types.

In the version of Swift Amit was using, however, Swift did convert it for him, and apparently it rounded up... Strange. Anyway, Apple have made it so you can't get that kind of unexpected behavior anymore: instead, it throws an error and you have to fix your code! You'll find Swift forces you to be very explicit compared to some other languages.

Thank you for your detailed answer. :) Only... now I have to reconsider (relearn??) a lot - I've been taught from this course (and the Stanford one I was trying before pushing pause there and moonwalking back to the beginners course here)... both courses teach that Swift is meant to be an Implicit Language - but from this error/example/etc it appears Swift is inconsistently Implicit... and apparently to compensate for the inconsistency I need to code as if Swift is just as Explicit as JS...

Oh... yay... okay...

Anyway, thank you for your help!