Protocol conformance in Swift - Time & Space Complexity
When we use protocols in Swift, we want to know how fast the program runs when it checks if a type follows a protocol.
We ask: How does the time to confirm protocol conformance change as we add more types or protocols?
Analyze the time complexity of the following code snippet.
protocol Greetable {
func greet()
}
struct Person: Greetable {
func greet() {
print("Hello!")
}
}
let p = Person()
p.greet()
This code defines a protocol and a struct that follows it, then calls the protocol method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the method defined by the protocol.
- How many times: Once in this example, but could be many times if called repeatedly.
When calling a protocol method, the time depends on how many calls you make, not on how many types conform.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 calls | 10 method calls |
| 100 calls | 100 method calls |
| 1000 calls | 1000 method calls |
Pattern observation: The time grows directly with the number of calls, not with the number of types or protocols.
Time Complexity: O(n)
This means the time grows linearly with how many times you call the protocol method.
[X] Wrong: "Checking if a type conforms to a protocol takes longer as more types conform."
[OK] Correct: Swift uses fast lookups for protocol conformance, so checking does not slow down with more types.
Understanding how protocol conformance affects speed helps you write clear and efficient Swift code, a skill valued in many coding challenges.
"What if we used a protocol with associated types? How would that affect the time complexity of calling methods?"
Practice
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]
- Thinking conformance means inheritance
- Believing only classes can conform
- Assuming methods must be overridden
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 BQuick Check:
Protocol conformance syntax uses ':' [OK]
- Using 'implements' or 'inherits' keywords
- Confusing inheritance with protocol conformance
- Using 'extends' which is not Swift syntax
protocol Greetable {
func greet() -> String
}
struct Person: Greetable {
func greet() -> String {
"Hello!"
}
}
let p = Person()
print(p.greet())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]
- Assuming missing implementation error
- Expecting default protocol method output
- Confusing print output with type description
Equatable protocol:struct Point: Equatable {
var x: Int
var y: Int
}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]
- Thinking manual == is always needed
- Believing structs can't conform
- Assuming properties must be let
Vehicle to conform to CustomStringConvertible and print a description?protocol CustomStringConvertible {
var description: String { get }
}
struct Vehicle {
var make: String
var model: String
}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]
- Trying to add description as a method
- Using stored property instead of computed
- Using override keyword in struct
Ready to prove it?
Pass the test and this pattern moves unknown
