W1036 Data Types

From Coder Merlin
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Whenever you save data to a variable, the computer needs to know what exactly the variable is storing (i.e. data types). This is important because a computer's memory is just a lot of ones and zeros; without knowing the type of data a variable holds then the computer has no way of knowing if '01000001' is representing the letter 'A' in ASCII (a character encoding) or the integer 65. Fortunately, higher-level programming languages abstract much of the behind-the-scenes memory stuff, so all you need to worry about as a programmer is using the appropriate data type for a variable.

Data types will also come in handy when creating and using functions, but we'll get to that later.

String[edit]

A string is a string of characters. In fact, in the programming language C, there isn't a built-in type for strings; you instead have to use an actual array of characters. In Swift, you can manually set a variable to the type string by using the type name String. Swift can also automatically guess the type in most circumstances.

var myStr : String = "This is a string" // myStr is of type String
var anotherStr = "This is another string" // anotherStr is automatically assigned the type of String

Numbers[edit]

Although Swift has quite a few different types for numbers, the main focus will be on integers (which store whole numbers) and the Double type, which is used to store decimals.

Integers[edit]

Integers, denoted by the type name Int, are used to store both positive and negative integers. Any math operations between two integers will not automatically result in any decimal places, and instead remove everything that would have been after the decimal place. For example, dividing the integer 10 by 3 would return 3 instead of 3.3.

var myNumber : Int = 150 // myNumber is of type Int
var anotherNumber = 112 // anotherNumber is also of type Int
var divResult = 10 / 3 // divResult is an Int with a value of 3

Doubles[edit]

Doubles are the type we'll use to store decimals in Swift.

It is worth noting that there's also a Float type that can store decimals with less precision. The advantage is that this uses less memory, but that's usually not a concern on modern computers, which usually have gigabytes of RAM (a Double takes up 64 bits, or 8 bytes; in other words, a Double uses 8e-7 percent of a single gigabyte).

var myDouble : Double = 150 // myNumber is of type Double
var anotherNumber = 120.0 // anotherNumber is also of type Double
var divResult = 10.0 / 3 // divResult is a Double with a value of 3.3333333333333335

Notice that by adding a decimal point, Swift will automatically assume a variable is a Double. When doing calculations, as long as any of the numbers involved have a decimal place, the result will also have decimal places.

Precision[edit]

To put it simply, Doubles aren't exact. For most use cases, this will be fine, but you may run into trouble if you need a large degree of precision. For example:

var divResult = 3.33 // divResult is a Double with a value of 3.3300000000000001

Boolean[edit]

The last data type you'll need to know about for the next few exercises is Bool, short for boolean. This data type holds either true or false, 1 or 0, or on or off depending on who you ask.

let myBool = true
let myOtherBool = false