TTS Swift Notes: Initialization Flashcards

1
Q

What is initialization?

A

The process of preparing an instance of a class, structure, or enumeration for use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What two steps does initialization involve?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you implement the initialization process?

A

By defining initializers which are like special methods that can be called to create a new instance of a particular type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What keyword is used for initializers?

A

init

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In its simplest form, an initializer is like a what?

A

An instance method with no parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two kinds of initializers that Swift defines for class types?

A

Designated and convenience initializers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What two things does a designated initializer do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does a convenience initializer do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When should you create a convenience initializer?

A

Whenever a shortcut to a common initialization pattern will save time or make initialization of the class clearer in intent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are convenience initializers written?

A

convenience init(parameters) {

statements

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the three rules of initializer delegation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Designated initializers must always delegate where?

A

Up.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Convenience initializers must always delegate where?

A

Across.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Class initialization in Swift is a how many phase process?

A

Two-phase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the first phase of class initialization?

A

Each stored property is assigned an initial value by the class that introduced it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the second phase of class initialization?

A

Each class is given the opportunity to customize its stored properties further before the new instance is considered ready for use.

17
Q

What two things does two-phase class initialization do?

A

1) Prevents property values from being accessed before they are initialized; and
2) Prevents property values from being set to a different value by another initializer unexpectedly.

18
Q

How many safety checks does Swift’s compiler perform to make sure that two-phase class initialization is completed without error?

A

Four.

19
Q

What are the four class initialization safety-checks?

A

1) A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer;
2) A designated initializer must delegate up to a superclass initializer before assigning a value to an inherited property. If it doesn’t, the new value the designated initializer assigns will be overwritten by the superclass as part of its own initialization.;
3) A convenience initializer must delegate to another initializer before assigning a value to any property (including properties defined by the same class). If it doesn’t, the new value the convenience initializer assigns will be overwritten by its own class’ designated initializer; and
4) An initializer cannot call any instance methods, read the values of any instance properties, or refer to self as a value until after the first phase of initialization is complete.

20
Q

If you write a subclass initializer that matches a superclass designated initializer, what modifier do you have to write before the subclass’ initializer definition?

A

override

21
Q

If your subclass doesn’t define any designated initializers, does it automatically inherit all of its superclass designated initializers?

A

Yes.

22
Q

If your subclass provides an implementation of all of its superclass designated initializers - either by inheriting them or by providing a custom implementation as part of its definition - then it automatically inherits what?

A

All of the superclass convenience initializers.

23
Q

For what three reasons might class initialization fail?

A

1) Invalid initialization parameter values;
2) the absence of a required external resource; or
3) some other condition

24
Q

How do you write a failable initializer?

A

(init?)

25
Q

What does a failable initializer create?

A

An optional value of the type that it initializes.

26
Q

What kind of a return do you write within a failable initializer?

A

return nil (to indicate a point at which initialization can be triggered)

27
Q

Enumerations with raw values automatically receive what?

A

A failable initializer ( init?(rawValue:)

28
Q

When can a class failable initializer trigger an initialization failure?

A

Only after all stored properties introduced by that class have been set to an initial value and any initializer delegation has taken place.

29
Q

How do you write a bailable initializer that creates an implicitly unwrapped optional instance of the appropriate type?

A

init!

30
Q

What keyword modifier is written before the definition of a class initializer to indicate that every subclass of the class must implement that initializer?

A

required

class SomeClass {
required init() {
// initializer implementation goes here
}
}