A data type is a type of data. It decides what type of data is being used or stored. Swift has tons of different data types, each that holds different information.
The variable is a central concept to programming. It is important that you understand it. Variables are used to store information that can be later used or changed in a computer program. They also provide a way of giving data a descriptive name, so our programs are easier to understand.
Think of variables as containers that hold something. That something is information. The purpose of these containers is to label and store data in memory. This data can then be used throughout your program. Also, the information in the container can always be modified or changed, hence the name, variable (like in Algebra).
In Swift, we create a variable writing var, giving it a name, followed by = and the information to fill the variable with. We will be going over the different data types below. Names are specific to the variable, so any prenamed code won’t work, same with numbers. However, we can write things like 2things or trees3.
You see/typed in this code:
import UIKit var str = "Hello, playground"
The import UIKit imports all the code necessary for the rest of our program to run. Most iOS/iPadOS apps are built on UIKit, so this is the framework we will be using. This gives us more freedom to create and do more.
The second line is a String. A string is a data type used in programming that is used to represent text. A string can consist of a set of characters including spaces and numbers. To create Strings, we just need to write the following, like we did before:
var message = "Hello world!"
Swift is smart, and has a feature called inferred types. This means that we don’t need to tell the code “This is a String". It automatically knows. However, sometimes we might need to tell it exactly what type it is. We can do this by adding a :String. This tells the code that this variable is a string. It also allows us to declare a variable and set the value later, as shown below.
var message: String message = "This is a message"
This sets message to “This is a message". But what if we want to change this afterwards? All we have to do is write this:
message = "This is still a message"
That applies to all variables. To give a variable a value in declaration or afterwards, just have an =, which means to set it.
Integers are a data type used for representing and storing, well, numeric values that are integers!!! (-49, -10, -2, 0, 3, 5, 459, etc). In programming languages, arithmetic operations (addition, subtraction, multiplication, division, etc) can be done as expected on data of this type. To create an Integer, it is super similar to Strings.
var num = 2 var numTwo: Int numTwo = 87 numTwo = -46
Floating point numbers are another data type. They are also used to represent numeric values like integers, but can have decimal values. They can also have arithmetic operations done on them. There are two main types that are used often in Swift, Doubles and Floats. The difference between the two is how precise they can get. Floats have the accuracy of about 7 decimal places and doubles can go to about 15.
var longitude: Double longitude = 36.1666672124356 var longitude: Float longitude = -186.7833
Booleans are a data type of two values: true or false. Just like the other data types, these are very easy to make.
var isItColdOutside = true var isIt2010: Bool var isIt2010 = false
We will dive further into these when we go over conditionals.
If we want to have a variable without having a specified type, we just need to use Any:
var something: Any something = 3 something = "tree"
Next, we will go over how we can print our information, and manipulate our data into Strings.