0
0
Swiftprogramming~5 mins

Initializers and designated init in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an initializer in Swift?
An initializer is a special method that prepares an instance of a class, struct, or enum for use by setting initial values for its properties.
Click to reveal answer
intermediate
What is a designated initializer?
A designated initializer is the primary initializer for a class that fully initializes all properties introduced by that class and calls an appropriate superclass initializer.
Click to reveal answer
intermediate
How does a designated initializer differ from a convenience initializer?
A designated initializer initializes all properties and calls a superclass initializer, while a convenience initializer provides shortcuts and must call another initializer from the same class.
Click to reveal answer
intermediate
Why must a designated initializer call a superclass initializer?
Because the superclass may have properties that need initialization, calling its initializer ensures the entire inheritance chain is properly set up.
Click to reveal answer
beginner
What happens if you don’t provide any initializer in a Swift class?
Swift provides a default initializer if all properties have default values and no custom initializers are defined.
Click to reveal answer
Which initializer must fully initialize all properties introduced by its class?
ADesignated initializer
BConvenience initializer
CDefault initializer
DFailable initializer
In Swift, a convenience initializer must call:
AA designated initializer from the same class
BA superclass initializer
CItself recursively
DNo other initializer
What is the main role of a designated initializer in a class hierarchy?
ATo deinitialize instances
BTo provide shortcuts for initialization
CTo initialize all properties and call the superclass initializer
DTo override superclass methods
If a Swift class has all properties with default values and no custom initializers, what initializer is available?
ADesignated initializer
BDefault initializer
CConvenience initializer
DNo initializer
Which of the following is true about designated initializers?
AThey can only be used in structs
BThey are optional
CThey cannot call other initializers
DThey must call a superclass designated initializer
Explain the difference between a designated initializer and a convenience initializer in Swift.
Think about who does the full setup and who provides easier ways to create instances.
You got /4 concepts.
    Describe why calling a superclass initializer is important in a designated initializer.
    Consider what happens if the superclass is not properly set up.
    You got /3 concepts.