Protocols define a set of rules or features that a type must follow. Protocol conformance means a type agrees to follow those rules.
Protocol conformance in Swift
Introduction
Syntax
Swift
protocol SomeProtocol { func someMethod() } struct SomeType: SomeProtocol { func someMethod() { // implementation } }
A protocol lists methods or properties without implementation.
A type (class, struct, enum) uses a colon and the protocol name to conform.
Examples
Swift
protocol Greetable { func greet() } struct Person: Greetable { func greet() { print("Hello!") } }
Swift
protocol Describable { var description: String { get } } class Car: Describable { var description: String { return "A fast car" } }
Swift
protocol Identifiable { var id: Int { get set } } struct User: Identifiable { var id: Int }
Sample Program
This program defines a Vehicle protocol with a startEngine method. Motorcycle conforms by implementing startEngine. When called, it prints a message.
Swift
protocol Vehicle { func startEngine() } struct Motorcycle: Vehicle { func startEngine() { print("Motorcycle engine started") } } let bike = Motorcycle() bike.startEngine()
Important Notes
All required methods and properties must be implemented to conform.
Protocols can be used as types to hold any conforming instance.
Conformance helps write reusable and organized code.
Summary
Protocols define rules for types to follow.
Types conform by implementing required methods and properties.
Protocol conformance enables flexible and clear code design.
Practice
1. What does it mean for a Swift type to conform to a protocol?
easy
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 CQuick 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
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 BQuick Check:
Protocol conformance syntax uses ':' [OK]
Hint: Use ':' after type name to conform to protocol [OK]
Common Mistakes:
- Using 'implements' or 'inherits' keywords
- Confusing inheritance with protocol conformance
- Using 'extends' which is not Swift syntax
3. What will be the output of this Swift code?
protocol Greetable {
func greet() -> String
}
struct Person: Greetable {
func greet() -> String {
"Hello!"
}
}
let p = Person()
print(p.greet())medium
Solution
Step 1: Check protocol and struct conformance
The protocol requires a greet() method returning String. Person implements greet() returning "Hello!".Step 2: Analyze print output
Calling p.greet() returns "Hello!", so print outputs that string.Final Answer:
Hello! -> Option AQuick Check:
Implemented greet() returns "Hello!" [OK]
Hint: Implemented method returns string printed directly [OK]
Common Mistakes:
- Assuming missing implementation error
- Expecting default protocol method output
- Confusing print output with type description
4. Identify the error in this Swift code that tries to conform to the
Equatable protocol:struct Point: Equatable {
var x: Int
var y: Int
}medium
Solution
Step 1: Understand Equatable conformance rules
Swift automatically synthesizes == for structs with Equatable properties.Step 2: Check struct properties and conformance
Both x and y are Int (Equatable), so compiler provides == automatically.Final Answer:
No error, code compiles fine -> Option DQuick Check:
Automatic Equatable synthesis = No error [OK]
Hint: Swift auto-synthesizes Equatable for simple structs [OK]
Common Mistakes:
- Thinking manual == is always needed
- Believing structs can't conform
- Assuming properties must be let
5. Given this protocol and struct, how can you modify
Vehicle to conform to CustomStringConvertible and print a description?protocol CustomStringConvertible {
var description: String { get }
}
struct Vehicle {
var make: String
var model: String
}hard
Solution
Step 1: Understand protocol property requirement
CustomStringConvertible requires a read-only computed property 'description' of type String.Step 2: Implement description correctly
Implementing description as a computed property in an extension is the correct way.Final Answer:
Add extension Vehicle: CustomStringConvertible { var description: String { "\(make) \(model)" } } -> Option AQuick Check:
Computed property in extension = Correct conformance [OK]
Hint: Use computed property in extension for protocol conformance [OK]
Common Mistakes:
- Trying to add description as a method
- Using stored property instead of computed
- Using override keyword in struct
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
