Swift Initialization
A Simple(ish) guide
Initialization is hugely important in any modern language. This is not a full guide (Apple have provided a long guide, which is referenced below) but rather a shorter example-laden walk through initialization for Swift.
Before we start
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 12.2, and Swift 5.3
Prerequisites:
- Some understanding of OO concepts
- Some idea of Structs and Classes in Swift
Terminology
Initializers: Called to create a new instance of a type, be it a struct, enum or a class.
Initializer delegation: Where an initializer calls other initializers to perform part of an instances initialization.
Initializers
Initializers for structs — the default initializer
An initializer can easily be created within a struct (note that the internal and external names are usually the same in an initializer)