Swift Types
In Swift there are two main categories of data types: value types and reference types.
Value types and Reference types
Value types are the predefined data structures provided by the language like ints, floats, chars, strings, bools, structs, arrays, dictionaries, tuples, enumerators and optionals. Reference types include classes, functions and function closures.
The main difference between value and reference data types is the way they are passed around in memory. When a value type is passed in to a function as a parameter, it is actually duplicated in memory and the duplicate is what is manipulated within the function; when the function ends, the copy is deleted. Reference types are handled differently, and pass a reference (pointer to the original object) around directly. This means the original object passed in to a function is changed when the function manipulates the passed-in value.
Types
Data types are the building blocks of programming languages and Swift has all the common types one expects to see in a modern language; however, Swift was created in part to get away from its more complex C-style predecessors to make writing applications faster, easier and in a more expressive way. For instance, characters in Swift are stored as a set of unicode scalar values called Extended Grapheme Clusters.
For an example of this, the unicode character for the letter “a” is "\u{61}” and can be combined into a scalar value like "\u{61}\u{302}" to give the character “â”. The root \u{61} and modifier \u{302} are combined to represent a single character—this differs from C with its ASCII character set—which leads to some interesting and expressive code. <!-- The following videos outline the similarities and differences between Objective-C and Swift data types, how Swift types are created and how they are accessed.
Type safety and inference Linkedin Learning
Swift operators Linkedin Learning -->
Strings
var firstString: String = "string 1" //assign a string explicitly
var secondString = "string 2" //inferred declaration
2
3
Characters
var firstChar: Character = "A" //assign a character explicitly
var secondChar = "B" //WARNING: inferred declaration defaults to string
2
3
Integers
var firstInt: Int = 1 //assign a integer explicitly
var secondInt = 2 //inferred declaration
2
3
Floating Points
var firstDouble: Double = 1.234 //assign a double explicitly
var secondDouble = 3.14159265359 //inferred declaration
2
3
var firstFloat: Float = 5.678 //assign a float explicitly
var secondFloat = 3.1415 //WARNING: all inferred floats are interpreted as doubles
2
3
NOTE: The compiler infers all floating point values as doubles unless explicitly declared as a float.
Booleans
var firstBool: Bool = true //assign a bool explicitly
var secondBool = false //inferred declaration
2
3
Numeric Literal Expressions
We have seen how to create and set whole and floating point numbers like:
var age: Int = 20
and
var height: Float = 211.5
...but if you had an integer variable representing a colour, it may be more advantageous to set it with a hexadecimal value. Setting an integer with a hexadecimal numeric literal value can be done like this:
var colorHex = 0xFF
Setting an integer with a binary numeric literal value can be done like this:
var colorBin = 0b11111111
Setting an integer with a octal numeric literal value can be done like this:
var colorOct = 0o377
Variable creation and deferred value assignment
As you have seen, in Swift the keywords var and let can be used when creating a new variable. The variable's type can be set through inference (like in JavaScript) when you set a value to that variable.
You can make a new integer variable called 'users' like this:
var users = 88
As you have seen, this is inferring the type (an Int) based on the value it is being set to.
Languages that only use inference are called loosely-typed languages. Swift is, in fact, a strongly-typed language, meaning the variable's type must be chosen when you create it.
The example "var users = 88" is actually considered a short-hand form for the explicit version of the same declaration, which looks like:
var users: Int = 88
Using the short-hand can only be used when initially assigning a value to your variable as you create it.
If you wanted to create a new variable but don't want to assign a value to it, you must assign the type like:
var users: Int
The following shows you examples of the different ways you can define a variable:
var firstString: String //deferred declaration of a string
firstString = "string 1" //deferred value assignment
let secondString: String //deferred declaration of a constant string
secondString = "string 2" //deferred constant value assignment
2
3
4
5
The following videos descibe the basics of creating variables in Swift:
Swift Fundamentals - Understanding Variables in Swift Pluralsight
Mutability with var
and let
So far in Swift you have seen how to create variables with the keyword var like this:
var height: Int = 20
...but what if you wanted to to create an immutable piece of data, like a const in JavaScript?
Using the keyword let instead of var creates your variable with read-only access; once an initial value is set can not be changed. The following shows you an example of how to define a constant integer using let.
let length: Int = 60
The next videos descibes constant variables, and why they are commonly used:
Swift Fundamentals - Constants: Why They're Important Pluralsight
Swift Fundamentals - Introducing Operators Pluralsight
Swift Fundamentals - Converting in Swift: When It Happens (And When it Doesn't)Pluralsight
Swift Fundamentals - Making Conversion Happen Pluralsight
For more information about Swift variables you can look at the Apple documentation here: