What's a guy in Swift

Data types

There are many types of data, and Swift treats each of them individually. You have already seen one of the most important types when you assigned text to a variable, but in Swift this type is called a character string.

Strings can be long (e.g. a million letters or more), short (e.g. 10 letters) or even empty (no letters), it doesn't matter: They are all strings from Swift's point of view and they all work the same way. Swift knows a string is because you assign a string to that variable when you create it: "Tim McGraw". If you rewritten your code in the following way, it would stop working:

This time, Xcode will give you an error message that doesn't make any sense at the moment: "Type annotation missing in pattern". That means something like: "I don't know what type of data is because you didn't give me enough information."

You now have two options: Either you create your variable and give it an initial value in the same line of code or you use the so-called type annotation, with which you tell Swift which data type this variable is storing, even if you are not giving it a value right now .

You're already familiar with the first option, so let's look at the second: Type Annotation. We know it will be a String, so we can tell Swift this by putting a colon and then after it, something like this:

In real app projects - unlike in playgrounds - this works very well because Swift knows what type of data will hold in the future. Playgrounds are a more special case, so you will still get an error message in the playground.

Note: Some people like to put a space before and after the colon, so, but that is not correct and you should avoid pointing out this mistake if you don't know people very well.

The lesson here is that Swift always wants to know what type of data a variable or constant will be. Always. You can't escape it, and that's a good thing because it provides something called "type safety" - if you say "this is a string" and later try to stick a rabbit in there, Swift will deny.

We can now try that out by adding another important data type called Int, Short form for "integer", that is, use a whole number. Whole numbers are numbers like 3, 30, 300, or -16777216. As an an example:

This creates one variable which is supposed to represent a string and another which is supposed to be an integer. Notice how and are capitalized, whereas and are capitalized - this is the standard Swift coding convention. A coding convention is something Swift doesn't care about (you can name your variables anyway!), But not other developers. In this case, data types are always capitalized, but not variables and constants.

Now that we have two variables of two different types, you can see type safety in action. Try to write the following:

In this code, you're trying to store an integer in a string variable and a string in an integer variable - and luckily, Xcode will throw up error messages. You might think that's petty, but it's actually pretty helpful: You make a promise that a variable will store a certain type of data, and Xcode will make sure you keep that promise.

Before you continue, please delete the incorrect two lines of code, otherwise nothing that comes afterwards in your playground will work!

Float and double

Let's look at two more data types, namely and. This is Swift's way of storing comma numbers, such as 3.1, 3.141, 3.1415926 and -16777216.5 (Translator's note: unlike in German, when programming comma numbers are written with an American instead of a German. This applies to the vast majority of programming languages.). There are two types of data for this because you can then choose how much accuracy you want to get, but most of the time it doesn't matter, which is why Apple's official recommendation is to always use it, as this offers the most accuracy.

Try to write the following in your playground:

You can see that both numbers appear on the right, but look carefully because there is a small discrepancy. We said that it should be -86.783333, but in the result column you see -86.78333 - there is a last 3 missing at the end. Now you might say "What is 0.000003 among friends" but that is exactly what I mean by accuracy.

Because playgrounds update as soon as you type, we can test exactly to what extent and differ. Try changing the code like this:

So we're adding numbers before the decimal point while leaving the rest of the number the same. However, if you look into the results panel, you will notice that Swift removes numbers after the period, while you insert numbers in front. This happens because there is limited space to store the numbers in, so the most important part of the numbers is stored first - messing around 1 million is a big deal, while messing around 0.000003 is less bad.

Now try to change to and you will see that Swift now returns the correct number every time:

This, as I said, is because is twice as accurate as, so Swift doesn't have to cut off the number at the end to make it match. Doubles still have their limits - if you were to store a huge number like 123456789.123456789, you would see that it is clipped to 123456789.12345679.

Boolean

Swift has a built-in data type that can store whether a value is true or false (that is, true or false). This is short for Boolean. Bools have no place for "maybe" or "likely", just absolute true or false. An example:

Use type annotations cleverly

As you've learned, there are two ways to tell Swift what type of data a variable is holding: assign a value when you create the variable, or use type annotation. If you have a choice, the first option is always better because it is more obvious. An example:

…is better than:

This applies to all data types. Example:

This technique is called type inference, that is, type inference, because Swift can infer which data type should be used for a variable by looking at the data that is stored there. For numbers like -86.783333, Swift will always infer instead of one.

For the sake of completeness, I should say that it is possible to specify a data type and assign a value at the same time, like this:

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