this taught me nothing, pick up your game lorenzo
Hi @thomasbigdawg and @anthonybigdawg, thanks for your comments. It would be helpful to elaborate more in order to improve for next tutorials.
I was finding it a bit hard to wrap my head around the following part (bold added by me):
var integer: Int = 100
var decimal: Double = 12.5
integer = Int(decimal)
If I understood correctly, what we’ve done here is change the value of the variable ‘integer’ from ‘100’ to ‘12’ …
I may be wrong, but was the goal here to change the ‘decimal’ variable? That’s what I’m inferring from the text that follows this code. If so, is this how we do it? :
var newDecimal = Int(decimal)
I had to create a new variable.
During operations, I see that the original variable can be used simply by adding the type that we want first, followed by the variable in parentheses like this example:
Double(integer) + decimal
This results in a double-type result.
Thanks for clarifying.
@triplespace The example describes how the Swift compiler enforces you to deal with correct types. In particular, when you do
var decimal: Double = 12.5, you fix the type of
decimal to be
Double. Then, if you try to change
integer (that is a
var of type
Int) with the value of
integer = decimal
the compiler won’t let you do so. Hence, the cast
Int(decimal) is mandatory to have the code compiling.
Hope it’s clear.
Thank you, it is now.
I didn’t realize that the goal was to change the value of the variable ‘integer’ to what the value of the variable ‘decimal’ was. I thought only the type change (from Int to Double) was being demonstrated.
This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!