TTS Swift Notes: Initialization Flashcards
What is initialization?
The process of preparing an instance of a class, structure, or enumeration for use.
What two steps does initialization involve?
Setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready to use.
How do you implement the initialization process?
By defining initializers which are like special methods that can be called to create a new instance of a particular type.
What keyword is used for initializers?
init
In its simplest form, an initializer is like a what?
An instance method with no parameters.
What are the two kinds of initializers that Swift defines for class types?
Designated and convenience initializers.
What two things does a designated initializer do?
They are the primary initializers for a class and they fully initialize all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.
What does a convenience initializer do?
They are the secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer’s parameters set to default values. You can also define a convenience initializer to create an instance of that class for a specific use case or input value type.
When should you create a convenience initializer?
Whenever a shortcut to a common initialization pattern will save time or make initialization of the class clearer in intent.
How are convenience initializers written?
convenience init(parameters) {
statements
}
What are the three rules of initializer delegation?
1) A designated initializer must call a designated initializer from its immediate superclass;
2) A convenience initializer must call another initializer from the same class; and
3) A convenience initializer must ultimately call a designated initializer.
Designated initializers must always delegate where?
Up.
Convenience initializers must always delegate where?
Across.
Class initialization in Swift is a how many phase process?
Two-phase.
What is the first phase of class initialization?
Each stored property is assigned an initial value by the class that introduced it.