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

Jonathan Dueck
Jonathan Dueck
4,596 Points

Error: type 'Int' does not conform to protocol 'FloatLiteralConvertible'. I had to use 12.0 & 10.0.

When I declared the constant areaInMeters, I got this console error:

Playground execution failed: <EXPR>:17:27: error: type 'Int' does not conform to protocol 'FloatLiteralConvertible' let areaInMeters = area / 10.764

I had to change my height and width to 12.0 and 10.0 respectively, but then of of course I got a non-integer answer.

What gives?

2 Answers

Ben Griffith
Ben Griffith
5,808 Points

As far as I'm aware - in order to be able to do any calculation, both need to be of the same type.

I found this in the Apple Guide to Swift under the chapters Language Guide -> The basics -> Integer and Floating-Point Conversion.

"Here, the value of the constant three is used to create a new value of type Double, so that both sides of the addition are of the same type. Without this conversion in place, the addition would not be allowed.”

let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneForOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double

Going back to your specific problem, alternative ways to solve it would be to cast the area to a double;

let areaInMeters = Double(area) / 10.764

You could then convert the constant areaInMeters to an Int;

let areaInMetersInteger = Int(areaInMeters)

Or do it all at once

let areaInMeters = Int(Double(area) / 10.764)
Dylan Bailey
Dylan Bailey
8,658 Points

Worked like a charm!

I guess the point is, why isn't this shown in the video. All it causes is massive amounts of frustration. Nothing worked until:

let areaInMeters = Int(Double(area) / 10.764)

Which wasn't built on at this point. All we were taught is area: Double

Also I don't get 12 as an answer, I get either 11.15034xxxxxxxxxxx without Int or 11 with Int.

Why these enormous differences from tiny amounts of code.

That makes sense, but in the video, he had it in as

let height = 12 //In Feet let width = 10 //In Feet

let area = height * width

//1 sq. meter = 1 sq. foot / 10.764 let areaInMeters = area / 10.764

and it just gave the answer of 12 instead of the error. I took a screen shot of them side by side and spent half an hour comparing every character- they were the same, and I got the error, he didn't. What gives?