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 Enumerations and Optionals Introduction to Enumerations Methods on Enumerations

Reed Carson
Reed Carson
8,306 Points

Why does he say we need to write 255.0 instead of jus 255 when the type has already explicitly been declared as a float?

why is he asking us to add the .0 when the types are already floats?

2 Answers

Nathan Tallack
Nathan Tallack
22,160 Points

Left over from legacy languages that did not like you trying to pass what looked like an int into a float or double.

Swift of course has no such problems. It is happy enough to automatically typecast an int into a float. :)

Reed Carson
Reed Carson
8,306 Points

so there is typecasting occurring under the hood in that instance?

Nathan Tallack
Nathan Tallack
22,160 Points

Well, in this instance I am guessing they declare Float so that they don't get people passing 55.0 and have it defaulting to a Double when type inference guess it as a double.

That could likely be the reason. Because if you just said let myFloat = 255.0 you would end up with a double. So they are explicitly declaring the type as a Float and seeing as you are doing that you can be lazy and pass in 255 and leave off the .0 if you wish. :)