KinderSwift 6: Types and Casting

In this lesson we cover how to explicitly type an identifier and how to use casting to force its type.

Transcript

Welcome back to kinderswift. episode 6. In our last lesson, we left off with this not working:

//can't mix types:
//doubleResult = myDouble + myInteger

Swift does not let you mix types. This lesson we will learn a little more about types and then how we can get this to work.

We already saw how to declare a double and an integer automatically. We call this implicit typing. In implicit typing, Swift guesses at the type.
so when we type:

var myInteger  = 0

the lack of a decimal point makes Swift guess this is an integer. Similarly

var myDouble = 0.0

the decimal point makes Swift guess that myDouble is type Double. We can also explicitly declare

var myInteger:Int = 5
var myDouble:Double = 5

You will see that Swift accepts both. myInteger is 5 and myDouble is 5.0 , even though we left off the decimal point myDouble is a Double. The difference is after the identifier we added a colon, then a type.
frame2
We know two types so far: Int for integers and Double for doubles. The colon forces Swift to make the identifier that type without guessing.

I can force a Int 5 to be a Double 5.0. Can I declare a Double as an Int? Try this:

var myOtherInteger:Int = 5.0

We get an error. Erase the decimal point and everything is fine. 

var myOtherInteger:Int = 5

No, we cannot assign a Double to an Int.

So for a little test what would this statement do?

myDouble = 3.5
myInteger = myDouble

Try it. type it in.

If you guessed we would get an error assigning a double to an integer, you are correct.
since you know that, you can be pretty certain this will also have an error:

myInteger = 4
myDouble = myInteger

What if you did want to assign integers to doubles and vice versa. What if you needed to add an integer to a double? There is a way called type casting. We take the value and force it to be a certain type. We do this by giving the type and then enclosing the value in parentheses
frame 3
comment out the error lines using // type this:

Int(4.5)
Double(3)

We see in the results we get a value of 4 and a value of 3.0. Int casting will cut off the part after the decimal point, Double just adds one. We can use casting on variables too. Uncomment the lines we just commented. Now change them to:

myDouble = Double(myInteger)

and

myInteger = Int(myDouble)

Now they work. Now we can fix our problem from before.

var doubleResult = myDouble + myInteger

How would you fix this?

We want a result of Double. We have a Double and an Int in the equation. Most likely we will change myInteger to Double like this.

var doubleResult = myDouble + Double(myInteger)

The way we cast Int() and Double() uses something called functions. In our next lesson we’ll discuss using functions a little bit more — including a few colorful ones.

One thought on “KinderSwift 6: Types and Casting”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s