Initialization Flashcards

1
Q

What is initialization?

A

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.

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

Failable Initializers:

What gets a default initializer?

A

Structs get a default initializer called memberwise initializer
And enums

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

Failable Initializers:

What indicates a failable initializer?

A

init?() {}

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

Failable Initializers:

Why would we use a failable initializer?

A

It will contain an optional if the initialization succeeds.

Or nil if the initialization fails

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

Failable Initializers:

How do you create an initializer that throws an error?

A

init() throws {}

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

Initializer Delegation:

When one initializer calls another this process is known as initializer __________

A

Delegation

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

Initializer Delegation:

What’s the purpose of having an initializer delegation?

A

So that we don’t repeat code and can specify something in the second or third initializer

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

Initializer Delegation:

Initializer delegation works differently for value types and reference types. How is that?

A

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

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

Initializer Delegation:

What is the primary initializer for a class called?

A

designated initializer

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

When initializing an object using a failable initializer the result is

A

An optional that either contains the object, if the initialization succeeded, or contains nil if initialization failed

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

A _______ initializer is responsible for assigning values to stored properties and calling the superclass’ initializer.

A

Designated

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

TF: A convenience initializer can call a designated initializer in a superclass

A

False - A convenience initializer can only call a designated initializer that is defined in the same class.

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

TF: A convenience initializer can call another convenience initializer defined in the same class

A

True

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

When one initializer calls another this process is known as initializer __________

A

Delegation

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

What’s the purpose of having required initializers?

A

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

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