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 Basics (retired) Operators Binary Operators

Dino A
seal-mask
.a{fill-rule:evenodd;}techdegree
Dino A
iOS Development Techdegree Student 786 Points

Passing on the variable

At around 6:14 Amit talks about passing on a variable to a double. Is there more than one way to write the code for that? For e.g. This is how it is in this lesson:

let areaInMeters = Double(area) / 10.764

Could you also write it like this:

let areaInMeters: Double = area / 10.764

much appreciated

2 Answers

Justin Black
Justin Black
24,793 Points

In short, no. It would still fail.

Reason being we are taking the area, which will always equal a whole number and as such is of Int type. In order to divide that by 10.764, we have to convert that to a double by typecasting the variable to be one.

Otherwise, you get the error: 'Int' is not convertible to 'Decimal'

Dino A
seal-mask
.a{fill-rule:evenodd;}techdegree
Dino A
iOS Development Techdegree Student 786 Points

I appreciate the reply Justin. I understand what you are saying. I just don't get why in the numbers "Numbers" course section you would write the code like this:

var version: Double = 1.0

But in "Binary operators" its written like this:

let areaInMeters = Double(area) / 10.764

Why isn't there a colin after areaInMeters like there is in the code above that? And why does area have to be in brackets? I guess what I am really asking is what is the difference the way both of those lines are written?

I hope this was clear. Its probably the simplest answer but its not computing.

cheers

Justin Black
Justin Black
24,793 Points

The first way would be called a 'type set' or typesetting. This is because you are explicitly telling the application that the variable must contain a double.

The 2nd is type-casting. You are taking the value of one type, and converting it to another. I imagine he did this more so as a reference to type-casting, without really explaining it well.

You could do it the way you suggest, but you'd have to do it for the height, width and area.