Swift: Types of variables – constants

After we have seen that we can store data in 2 types of references: variables and constants (Swift: Variables and Constants), we will now take a look at what type of data can we store and how to correctly declare it.

You should keep in mind, that in Swifts, variable / constants declarations can be done directly and indirectly. What I mean is that, Swift will recognize the value type and “auto-declare” itself, you don’t need to do it explicitly. So let’s look at an example:

var num = 2

In this example, Swift recognizes that the variable named “num” is a whole number so it will declare it as an “Integer” type. However, for beginners, we recommend you to always declare your variables directly as we will show you below. This can avoid some bugs and your code is cleaner and much easier to read.

The different types that exist in Swift are:

1. String

var object : String = “Phone”

String type is used for text. So, when you have a value that is a word or a sentence, declare it as a string.

When declaring the variable as a string, pay attention to they way you write it. Indeed, use a capital “S” when you write “String” or else Swift won’t recognize it.

The text that will be contained by the variable or the constant, needs to be between brackets. Note that this is only the case with Strings.

You can even declare a String as empty as follows:

var emptystring : String = “”

2. Int

Int type is used for whole numbers. Again, when declaring an “Int”, put in an capital “I” as shown below:

var number : Int = 4


3. Double

Double type is used for numbers with decimals. We will see later on that in order to do some operations with numbers, we will either need to have the same type or convert one of the type.

var pi : Double = 3.14


4. Float

Float type is very similar to double, we use them for numbers with decimals. However, you may ask yourself, if they were the same, why would there be another class type? And you are absolutely right… Well, float and double may be very similar but there is a slightly big difference which consists in the precision: double (64-bit number) is much more accurate than float (32-bit number). So most of the time, if you want accuracy, work with doubles instead of floats.

var height : Float = 1.74


5. Bool

Boolean type can only have two values: either true or false. They are very useful for logical conditions such as if, that we will see later on.

var sunny : Book = True


6. Char

Character type is basically a single character string. So a variable is declared as a character the following way:

var alphabet : Char = “A”

I would like to stress out that a character cannot be declared as empty, as in the case of a String, this will produce an error.

The next tutorial will teach you how to combine the different types of data and show it as an output.


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

Blog at WordPress.com.

Up ↑

%d bloggers like this: