TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Double and Int numbers

Forums > SwiftUI

Hello, on Day 1, when talking about the difference between Int and Double numbers, it is said that both cannot operate on each other, unless specifically specified. However, when talking about being able to use compound assignment operators, the example given is the following: var rating = 5.0 rating *= 2

Aren't you supposed to be able to do that without using Double() or Int()?

Thank you!

2      

Given code like:

var rating = 5.0
rating *= 2

the Swift compiler is smart enough to know that since rating is a Double (because it was initialized as 5.0) then it should interpret the 2 in rating *= 2 as a Double as well. It can't be an Int because you cannot multiple a Double by an Int without converting one of them, so 2 has to be a Double, in which case rating *= 2 is the same as writing rating *= 2.0

In situations where it is unambiguous as to whether a Double or an Int is meant, the compiler can figure it out for itself.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.