Protocol declaration syntax in Swift - Time & Space Complexity
Let's see how the time to declare a protocol changes as the protocol grows.
We want to know how adding more requirements affects the work done when declaring a protocol.
Analyze the time complexity of the following code snippet.
protocol Vehicle {
var numberOfWheels: Int { get }
func startEngine()
func stopEngine()
func drive(distance: Double)
}
This code declares a protocol named Vehicle with some properties and methods that any conforming type must implement.
Look for parts that repeat or grow with input size.
- Primary operation: Listing each property and method requirement inside the protocol.
- How many times: Once for each requirement added to the protocol.
As you add more requirements, the work to declare the protocol grows in a simple way.
| Input Size (n) | Approx. Operations |
|---|---|
| 4 | 4 operations (4 requirements) |
| 10 | 10 operations |
| 100 | 100 operations |
Pattern observation: The work grows directly with the number of requirements you add.
Time Complexity: O(n)
This means the time to declare a protocol grows linearly with the number of requirements it has.
[X] Wrong: "Declaring a protocol always takes the same time no matter how many requirements it has."
[OK] Correct: Each requirement adds a small amount of work, so more requirements mean more time to declare.
Understanding how code size affects work helps you write efficient and clear protocols, a skill useful in real projects and interviews.
"What if the protocol inherits from multiple other protocols? How would the time complexity change?"
Practice
Solution
Step 1: Recall Swift syntax for protocols
In Swift, protocols are declared using the keywordprotocol.Step 2: Compare options with known syntax
Options likeinterfaceare from other languages, andclassorstructdeclare types, not protocols.Final Answer:
protocol -> Option CQuick Check:
Keyword for protocol declaration = protocol [OK]
- Confusing 'protocol' with 'interface' from other languages
- Using 'class' or 'struct' instead of 'protocol'
- Misspelling the keyword
Vehicle in Swift?Solution
Step 1: Understand protocol declaration syntax
Protocols are declared with the keywordprotocol, followed by the name and curly braces without parentheses or brackets.Step 2: Check each option
protocol Vehicle {} uses correct syntax:protocol Vehicle {}. Options B, C, and D incorrectly add parentheses or brackets.Final Answer:
protocol Vehicle {} -> Option AQuick Check:
Protocol declaration uses curly braces only [OK]
- Adding parentheses after protocol name
- Using square or angle brackets incorrectly
- Omitting curly braces
protocol Greetable {
func greet() -> String
}
struct Person: Greetable {
func greet() -> String {
return "Hello!"
}
}
let p = Person()
print(p.greet())Solution
Step 1: Understand protocol and struct implementation
The protocolGreetablerequires a methodgreet()returning a String. The structPersonimplements this method returning "Hello!".Step 2: Analyze the print statement
Creating an instancepofPersonand callingp.greet()returns "Hello!", which is printed.Final Answer:
Hello! -> Option AQuick Check:
Implemented protocol method returns "Hello!" [OK]
- Assuming protocol methods run automatically without implementation
- Expecting error due to protocol conformance
- Confusing method name with output
protocol Drawable {
func draw()
var color: String
}Solution
Step 1: Check protocol property declaration rules
In Swift, protocol properties must declare if they are readable or writable using{ get }or{ get set }.Step 2: Analyze the given property
The propertycolor: Stringlacks{ get }or{ get set }, causing a syntax error.Final Answer:
Protocol properties must specify { get } or { get set } -> Option BQuick Check:
Protocol property syntax requires access specifiers [OK]
- Omitting { get } or { get set } in protocol properties
- Adding parentheses to property declarations
- Thinking protocol names must be uppercase
Identifiable that requires a read-only id property of type String and a method identify() that returns a String. Which is the correct declaration?Solution
Step 1: Define read-only property in protocol
A read-only property in a protocol must be declared with{ get }to indicate it can be read but not written.Step 2: Check method signature
The methodidentify()must specify return typeStringas required.Step 3: Evaluate options
protocol Identifiable { var id: String { get } func identify() -> String } correctly declaresvar id: String { get }andfunc identify() -> String. protocol Identifiable { var id: String func identify() } misses{ get }and return type. protocol Identifiable { let id: String func identify() -> String } usesletwhich is invalid in protocols. protocol Identifiable { var id: String { get set } func identify() -> String } allows writing with{ get set }, not read-only.Final Answer:
protocol Identifiable { var id: String { get } func identify() -> String } -> Option DQuick Check:
Read-only property uses { get } and method return type matches [OK]
- Using 'let' instead of 'var' in protocol
- Omitting { get } for read-only properties
- Missing return type in method declaration
Ready to prove it?
Pass the test and this pattern moves unknown
