Initialization Flashcards
What is initialization?
It’s the process of preparing an instance of a class, structure, or enumeration for use by setting up initial values to all stored properties and performing any other set up.
Their primary role is to ensure that new instances of a type are correctly initialized before they are used for the first time.
Failable Initializers:
What gets a default initializer?
Structs get a default initializer called memberwise initializer
And enums
Failable Initializers:
What indicates a failable initializer?
init?() {}
Failable Initializers:
Why would we use a failable initializer?
It will contain an optional if the initialization succeeds.
Or nil if the initialization fails
Failable Initializers:
How do you create an initializer that throws an error?
init() throws {}
Initializer Delegation:
When one initializer calls another this process is known as initializer __________
Delegation
Initializer Delegation:
What’s the purpose of having an initializer delegation?
So that we don’t repeat code and can specify something in the second or third initializer
Initializer Delegation:
Initializer delegation works differently for value types and reference types. How is that?
Well structs don’t need an initializers but classes do.
Beyond that, if you are delegating for a struct, you just use inits, but for a class, you need convenience initializers beyond the first init
Initializer Delegation:
What is the primary initializer for a class called?
designated initializer
When initializing an object using a failable initializer the result is
An optional that either contains the object, if the initialization succeeded, or contains nil if initialization failed
A _______ initializer is responsible for assigning values to stored properties and calling the superclass’ initializer.
Designated
TF: A convenience initializer can call a designated initializer in a superclass
False - A convenience initializer can only call a designated initializer that is defined in the same class.
TF: A convenience initializer can call another convenience initializer defined in the same class
True
When one initializer calls another this process is known as initializer __________
Delegation
What’s the purpose of having required initializers?
When we write the required modifier before the definition of a class initializer, we are basically requiring every subclass of the class to implement that initializer