Typealias
Introduction
Welcome to the last lesson of The Swift Fundamentals. You’ve come a long way. You will learn how to create fake names for anything. This lesson should be nice and short.
Problem
The parameters are unreadable and boring
Typealias: A false or assumed identity. - Oxford Dictionary
Typealias for String
Instead of using String
, let us create a fake name for String
.
typealias Name = String
func insertName(name: Name) {}
insertName(name: "Bob Lee")
Typealias for Custom Class
class Employee {}
typealias MyEmployees = [Employee]
func listEmployees(enterEmployees: MyEmployees) {}
Typealias for Tuple
typealias GridPoint = (Int, Int)
func enterPoint(grid: GridPoint) {
print("You've entered, \(grid.0) and \(grid.1)")
}
enterPoint(grid: (4, 2))
Type Definition
Let’s review how to initialize Array
, Dictionary
, and Optional
.
Array Type
let arrayOne: Array<String> = ["Bob", "Bobby"] // Generic Struct
let arrayTwo: [String] = ["Bob", "Bobby"]
Dictionary Type
let dictTwo: Dictionary<String, Int> = ["Alex": 31, "Paul": 39] // Generic Struct
let dictOne: [String: Int] = ["Alex": 31, "Paul": 39]
Optional Type
var optionalOne: String?
var optionalTwo: Optional<String> // Generic Enum
You will learn more about
optionals
in Chapter 8: Advanced Enums.
Source Code
Conclusion
That’s it. You’ve learned how fake.