Chapter 5.  Factories

This chapter will introduce you to the concept of types. A data type, or simply a type, is a classification of data. The type defines the operations that can be done on the data and the various attributes it might have. A sentence or a word can be considered as being a String. A number (non-decimal) can be considered to be an Int type. We will cover what exactly String and Int are along with how to create our own instances of String and Int. An instance is the actual number or the actual word we're referring to.

In this chapter, we will cover the following topics:

There are only a few things on this Earth that are better than ice cream. we're just having trouble naming them. How is ice cream actually made? Well, it is made in an ice cream factory. This factory is able to create different flavors and toppings.

What about cars, how are cars made? Similar to ice cream, a car is made in a car factory. A car factory can create all kinds of different cars that we can drive around.

In Swift, there are many factories built into the language that are available for us to use. However, Swift doesn't refer to them as factories, it refers to them as types

When a car factory creates a car, the car that is now in your possession is referred to as an instance of car (in Swift, types begin with an uppercase letter). Having your own instance of a car is like having a car in your driveway that you're able to use whenever you want, it's yours! You could drive it, park it, change its color, turn left, turn right, and play music. All of these actions are referred to as functions in Swift. Functions are actions that you can perform with instances of types.

Swift has different types that we can create instances of, which will help us create awesome applications.

The first type that we will talk about is the String type. Again, think of the String type as a factory that can create instances of String for us to use in our app. What does an instance of String actually look like? The following are three examples of String instances:

"Hello World"
"Frodo is the best hobbit!"
"I'm so hungry, I can eat an entire pizza."

You've been working with instances of String all along and you didn't even know it! Lets take this a step further:

let favoriteColor = "Blue"

This is code that you've seen before. In fact, you've written code just like this already! We just learned, in the last chapter, that variables and constants store values. It means that wherever we use favoriteColor in our code, we're actually referring to the value Blue because it's storing this value for us wherever it goes. Every value in Swift is an instance of some type. Now, knowing this, what would you guess is the type of favoriteColor.

The favoriteColor constant is of the String type. To confirm this, while holding down the option key on your keyboard, we will select the favoriteColor constant in our playground file to be met with the following tooltip:

String and Int

Note what's to the right of the word Declaration-let favoriteColor: String.

This can be read like so: here we've declared a constant, called favoriteColor, of the String type. Anytime you see a colon (:) , it can be replaced with the words of type.

How did Swift know that favoriteColor was of the String type. We didn't specify that it was of the String type, we just typed out some characters within double quotes. That's precisely how Swift was able to figure out that it was an instance of String. This process is known as type inference. Surrounding characters in double quotes is formally known as a string literal and when Swift sees this code, it's able to immediately tell that it's a String and properly label the variable or constant as being of that type.

Here are some more examples of using type inference in Swift to create some variables:

let name = "Jim"
let catName = "Hanna"
var favoriteFood = "Pasta"

We've created a constant, called name, of the String type, assigning it the value Jim. We've also created a constant, called catName, of the String type, assigning it the value Hanna. Lastly, we've created a variable, called favoriteFood , of the String type, assigning it the value Pasta.

Can you create instances of String without using type inference? Yes.

Instead of taking advantage of type inference in your creation of a variable or constant, you can be explicit with the type information, as shown:

let momsName: String = "Maryann"

Here, we've created a constant, called momsName, of the String type, assigning it the value Maryann. If we were to hold the option key and click on the momsName constant, we would also see that it's of the String type:

String and Int

This comes in very handy when you're looking to create a variable that will ultimately store a value for you that you don't need immediately. What does that mean?

Imagine that you're creating an app that will ask everyone in your family what they want to eat for dinner. You might create the following constant:

let question = "What do you want to eat for dinner?"

Where will we store the answer? Let's create a variable that will store their response:

var answer: String

answer is a variable of the String type. However, you might have noted that we haven't yet assigned it a value. You may say that it's valueless, that is, it doesn't have any value at declaration. If we try to use this answer variable before assigning it a value, our application will crash.

Let's see that in action; we will attempt to print the answer variable only to be met with a crash in our playground:

String and Int

The error states that the answer variable is used before being initialized. If we want to get rid of this error, we need to assign a value to this answer variable. Let's pretend that our sister gave us a response, which we can now store in this variable. we will assign the value Cake to the answer variable because, of course, that's what she wants to eat for dinner:

String and Int

This is pretty neat. What happened here is that the compiler is making sure that we have values assigned to our constants or variables before we use them. Variables or constants must have their types known at declaration but don't need to have an initial value (as we did earlier). This is one of the many fundamental building blocks that will allow us to build versatile applications in Swift.

So far, we've only dealt with one type-the String type. However, if we lived in a world where there was only an ice cream factory producing ice cream, we would get very sick (although we would still want to live in that world). There are different kinds of factories producing all sorts of things that we can use in our day-to-day life; the same can be said for Swift. If we want a donut, we won't walk up to the ice cream factory, we will go to the donut factory.

In Swift, if you want a number, there are different types (or factories) that we can utilize to create the type of number we want.

You can easily create numbers in Swift as follows:

var age = 75

In creating this age variable, we've assigned it the value 75. Note how there aren't any double quotes around 75. If there were, it would instead be of the String type. Writing out a number as shown by itself (with no double quotes) is producing a number, not a String. So, if we were to hold down the option key and click on the age variable, what would be its type?

String and Int

It's of the Int type. As it's an Int (short for integer), we can perform math operations on it, unlike a String:

String and Int

We've included the result of the math operation as follows. Let's walk through the first one,  age + 5 . Note how age is a variable of the Int type. It's represented by its value (which is 75). The mathematical operation of age + 5 can be broken down as 75 + 5 (because you replace your variable or constant with its value).

You might have noted that we're taking advantage of type inference again. Swift is able to figure out the type we intend to use without us having to be explicit about it. However, similar to creating String, we can be explicit if we want to:

var hitPoints: Int = 95
let planetsInSolarSystem: Int = 8

hitPoints is a variable of the Int type with a value of 95. planetsInSolarSystem is a constant of the Int type with a value of 8.