Recall & Review
beginner
What is a protocol in Swift?
A protocol in Swift is like a blueprint that defines methods and properties a type must have. It tells what to do, but not how to do it.
Click to reveal answer
beginner
Can protocols require both methods and properties?
Yes, protocols can require methods (functions) and properties (variables) that conforming types must implement.
Click to reveal answer
intermediate
What does it mean when a protocol property is declared with { get set }?
It means the property must be readable and writable by the conforming type.
Click to reveal answer
beginner
How do you declare a method requirement in a Swift protocol?
You write the method signature without a body inside the protocol. For example: <br>
func start()Click to reveal answer
intermediate
Can protocol requirements specify if a property is a variable or constant?
No, protocols only specify if a property is gettable or settable. The conforming type decides if it uses var or let.
Click to reveal answer
What must a type do when it conforms to a protocol with property requirements?
✗ Incorrect
A type must implement all required properties with the correct get/set access as specified by the protocol.
How do you declare a read-only property requirement in a protocol?
✗ Incorrect
Using { get } means the property must be readable but not necessarily writable.
Which of these is a valid method requirement in a Swift protocol?
✗ Incorrect
Protocol method requirements only declare the signature without a body.
If a protocol requires a property with { get set }, can a conforming type implement it as a constant (let)?
✗ Incorrect
Properties with { get set } must be writable, so they cannot be constants.
What happens if a type does not implement all protocol requirements?
✗ Incorrect
Swift requires all protocol requirements to be implemented; otherwise, it causes a compilation error.
Explain what protocol requirements are in Swift and how they apply to methods and properties.
Think about what a protocol asks a type to provide.
You got /4 concepts.
Describe the difference between { get } and { get set } in protocol property requirements.
Focus on property access levels.
You got /4 concepts.