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 that a Swift struct conforms to a protocol?
You add the protocol name after the struct name, separated by a colon. For example: struct MyStruct: MyProtocol { }
Click to reveal answer
beginner
What happens if a type does not implement all requirements of a protocol it claims to conform to?
The Swift compiler will give an error because the type does not fully conform to the protocol requirements.
Click to reveal answer
intermediate
Can a class conform to multiple protocols in Swift? How?
Yes, a class can conform to multiple protocols by listing them separated by commas after the class name. For example: <code>class MyClass: ProtocolA, ProtocolB { }</code>
Click to reveal answer
intermediate
What is the benefit of using protocol conformance in Swift?
It allows different types to share common behavior without inheritance, enabling flexible and reusable code. It also helps with abstraction and writing generic code.
Click to reveal answer
How do you declare that a Swift class conforms to a protocol named 'Drivable'?
Aclass Car inherits Drivable { }
Bclass Car implements Drivable { }
Cclass Car extends Drivable { }
Dclass Car: Drivable { }
✗ Incorrect
In Swift, you use a colon ':' to declare protocol conformance, not 'implements', 'inherits', or 'extends'.
What must a type do to conform to a protocol?
AImplement all the protocol's required methods and properties
BOnly declare the protocol name
CInherit from the protocol
DOverride the protocol's methods
✗ Incorrect
A type must implement all required methods and properties defined in the protocol to conform.
Can a struct conform to a protocol in Swift?
ANo, only classes can conform
BOnly enums can conform
CYes, structs can conform to protocols
DProtocols cannot be conformed to
✗ Incorrect
In Swift, structs, classes, and enums can all conform to protocols.
What symbol is used to separate multiple protocols in a conformance list?
A; (semicolon)
B, (comma)
C& (ampersand)
D| (pipe)
✗ Incorrect
Multiple protocols are separated by commas after the type name.
Why use protocols instead of inheritance?
AProtocols allow multiple conformances and more flexible design
BProtocols are slower than inheritance
CProtocols require less code
DProtocols replace functions
✗ Incorrect
Protocols allow types to conform to multiple protocols, enabling flexible and reusable code beyond single inheritance.
Explain how a Swift type conforms to a protocol and what happens if it does not implement all required members.
Think about the syntax and compiler checks.
You got /3 concepts.
Describe the benefits of using protocol conformance in Swift programming.
Consider how protocols help organize and share behavior.
You got /5 concepts.
Practice
(1/5)
1. What does it mean for a Swift type to conform to a protocol?
easy
A. It can only be a class, not a struct or enum.
B. It inherits all properties and methods from the protocol automatically.
C. It implements all required methods and properties defined by the protocol.
D. It must override all methods from the protocol.
Solution
Step 1: Understand protocol requirements
A protocol defines methods and properties that a type must implement to conform.
Step 2: Check what conformance means
Conforming means the type provides implementations for all required members.
Final Answer:
It implements all required methods and properties defined by the protocol. -> Option C
Quick Check:
Protocol conformance = Implement required members [OK]
Hint: Conformance means implementing all required protocol parts [OK]
Common Mistakes:
Thinking conformance means inheritance
Believing only classes can conform
Assuming methods must be overridden
2. Which of the following is the correct syntax to declare that a struct conforms to a protocol named Drawable?
easy
A. struct Shape inherits Drawable { }
B. struct Shape : Drawable { }
C. struct Shape implements Drawable { }
D. struct Shape extends Drawable { }
Solution
Step 1: Recall Swift syntax for protocol conformance
Swift uses a colon (:) after the type name to declare protocol conformance.
Step 2: Match syntax options
Only struct Shape : Drawable { } uses the correct colon syntax for protocol conformance.
Final Answer:
struct Shape : Drawable { } -> Option B
Quick Check:
Protocol conformance syntax uses ':' [OK]
Hint: Use ':' after type name to conform to protocol [OK]