This tutorial will each you how Variables & Constants work in Swift work and will also show you the various datatypes.
Whats a Variable?
Each variable in Swift has a specific type, which determines the size and layout of the variable’s memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable. You can declare a variable using the keyword ‘var’ like so:
var number: Float
So.. Whats a Constant?
A constant is a value that cannot be changed, meaning this will remain the same value thought the code. You can declare a constant to any datatype just like a variable but you must always initialize your let constants, even if you explicitly type them. You can declare a variable using the keyword ‘let’ like so:
let name = jack;
Int – This type is commonly used for whole numbers.
var age = 19
Float − This is used to represent a 32-bit floating-point number.
var moneyOwed = 7.20
Double − This is used to represent a 64-bit floating-point number.
var weight = 23.4432344
Bool − This represents a Boolean value which is either true or false.
var isUserLoggedIn = true
String − This is an ordered collection of characters.
var name = “Jack”
Character − This is a single-character string literal.
var character = “c”
The name of a variable can be name with letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Swift 4 is a case-sensitive programming language.