Recall & Review
beginner
What is a protocol in Swift?
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. Classes, structs, or enums can adopt protocols to provide actual implementations.
Click to reveal answer
beginner
How do you declare a protocol in Swift?
Use the
protocol keyword followed by the protocol name and curly braces. Inside, list the required methods or properties without implementations.Click to reveal answer
intermediate
Can a Swift protocol require properties? If yes, what kind?
Yes, protocols can require properties. They can specify whether the property must be gettable or gettable and settable, but they do not provide storage or implementation.
Click to reveal answer
beginner
What does it mean for a type to conform to a protocol?
It means the type agrees to implement all the requirements defined in the protocol, such as methods and properties, ensuring it meets the protocol's contract.
Click to reveal answer
intermediate
Can protocols be used as types in Swift? Give an example.
Yes, protocols can be used as types to define variables, parameters, or return types. For example, a variable of type
MyProtocol can hold any instance of a type that conforms to MyProtocol.Click to reveal answer
Which keyword is used to define a protocol in Swift?
✗ Incorrect
Swift uses the keyword
protocol to define protocols.What must a type do to conform to a protocol?
✗ Incorrect
Conforming means implementing all the protocol's required methods and properties.
Can a protocol specify property requirements without providing storage?
✗ Incorrect
Protocols specify whether properties must be gettable or settable but do not provide storage.
Which of these can adopt a protocol in Swift?
✗ Incorrect
Classes, structs, and enums can all adopt protocols.
Can you use a protocol as a type for a variable?
✗ Incorrect
Protocols can be used as types to hold any instance that conforms to them.
Explain what a protocol is in Swift and why it is useful.
Think about how protocols help different types agree on a set of rules.
You got /4 concepts.
Describe how a type conforms to a protocol and what happens if it does not implement all requirements.
Consider what the compiler expects when you say a type adopts a protocol.
You got /3 concepts.