Initializers in Swift — Part 1/3 (Structures and enumerations)

Initializers in swift is a very important and vast topic. So would like to write it in 3 parts containing introduction, overview in one part and initializers for class in a separate part as it is huge and failable initializers and closures in the initializers in the last part.
This write up will be mostly important points written as a notes so that you can use it while revising the concept.
Let’s get started..
Introduction
- Initialization is the process of preparing an instance of a class, structure, or enumeration for use.
- This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that’s required before the new instance is ready for use.
- When you create an object in Swift, a special method known as its initializer with
init
keyword is called.
Initializers:
There are different types of initializers:
- Default initializers for classes, structures and enumerations.
- Memberwise initializers for structure types.
- Designated and Convenience for Classes.
- Failable initializers for class, structure and enumerations.
We will discuss more about default and initializers related to structures in this part.
Default Initializers:
The default initializer simply creates a new instance with all of its properties set to their default values.
class ShoppingListItem {
var name: String?
var quantity = 1
var purchased = false
}
var item = ShoppingListItem()
Because all properties of the
ShoppingListItem
class have default values, and because it’s a base class with no superclass,ShoppingListItem
automatically gains a default initializer implementation that creates a new instance with all of its properties set to their default values.
Memberwise Initializers for Structure Types:
- Structure types automatically receive a memberwise initializer if they don’t define any of their own custom initializers.
- Unlike a default initializer, the structure receives a memberwise initializer even if it has stored properties that don’t have default values.
struct Size {
var width = 0.0, height = 0.0
}
let twoByTwo = Size(width: 2.0, height: 2.0)
let zeroByTwo = Size(height: 2.0)
print(zeroByTwo.width, zeroByTwo.height)
// Prints "0.0 2.0"
let zeroByZero = Size()
print(zeroByZero.width, zeroByZero.height)
// Prints "0.0 0.0"
Initializer Delegation for Value Types
- Initializers can call other initializers to perform part of an instance’s initialization.
- This process, known as
initializer delegation
, avoids duplicating code across multiple initializers. - Value types (structures and enumerations) don’t support inheritance, and so their initializer delegation process is relatively simple, because they can only delegate to another initializer that they provide themselves.
- Classes, however, can inherit from other classes. This means that classes have additional responsibilities for ensuring that all stored properties they inherit are assigned a suitable value during initialization.
- For value types, you use
self.init
to refer to other initializers from the same value type when writing your own custom initializers. You can callself.init
only from within an initializer. - Example