Protocol Flashcards

1
Q

What is a protocol and why use it?

A

A blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.

It’s a great way to make code sexier by having something that would have been used multiple times in various places, be accessible from one area.

Protocols can also serve as types.

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

What is a gettable property?

A

A property you can read but not set it after we’ve given it an initial value

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

How you do you get a computed property?

A

You get a computed property value by doing some computation

It had no storage

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

TF: Protocols can require that conforming types have specific instance properties, instance methods and type methods.

A

True

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

If a stored property requirement in a protocol is declared as gettable and settable, the property cannot be implemented as a:

A

Constant, because they are not settable

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

What is a protocol?

A

A contract of methods, properties, and other requirements

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

How can protocols serve as types?

A

As a parameter type or return type in a function, method or initializer
As the type of a constant, variable, or property
As the type of items in an array, dictionary, or other containers

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

TF: Protocols are types just like classes and structs?

A

True

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

TF: == is a protocol?

A

True

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

TF: Protocols describe what should be implemented but does not require a specific implementation.

A

True

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

When trying to decide whether or not to use a protocol, how can you implement is-a and has-a relationships?

A

Class Airplane and Class Jetplane are is-a because a jet plane is a type of airplane. → when we have a is-a relationship, Inheritance is best for design pattern

Class Airplane and struct Bird are a has-a because they are not really related, but they both can fly. → when we have a has-a relationship, we should use a fly protocol in this case.

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

Protocols typically end with what suffix?

A

Able

But this isn’t always true

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

For protocols, how do you set up a property?

A
Either var or let
name 
Type 
And whether it is gettable or gettable and settable 
I.e → var firstName: String { get set }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are type methods?

A
Methods that are defined on the type itself rather than an instance
Need to use static before the name of method to use it in class or struct
In example below, the method is defined in the func itself, not before. 

struct User: PrettyPrintable, Equatable {
let name: String
let age: Int
let address: String

static func ==(lhs: User, rhs: User) -> Bool {
    return lhs.name == rhs.name && lhs.age == rhs.age && lhs.address  == rhs.address
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does AnyObject vs Any represent?

A
AnyObject - An instance of any class type 
Any - can represent anything including functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a great way to determine whether or not to use classes vs structs?

A

Value types like structs are things

Reference types like classes do things.

17
Q

TF: Multiple protocol inheritance allows composition of functionality by combining types as you need

A

True