Protocols let us define a set of rules or features. Using a protocol as a type means we can work with any object that follows those rules, without caring about its exact kind.
Protocol as types in Swift
protocol SomeProtocol { func doSomething() } func useProtocolType(item: SomeProtocol) { item.doSomething() }
You use the protocol name as a type to accept any object that conforms to it.
This helps write flexible and reusable code.
Greetable is a protocol. The function sayHello accepts any object that can greet.protocol Greetable { func greet() } struct Person: Greetable { func greet() { print("Hello!") } } func sayHello(to someone: Greetable) { someone.greet() }
Drawable objects. Each can be drawn, even if they are different shapes.protocol Drawable { func draw() } class Circle: Drawable { func draw() { print("Drawing a circle") } } let shapes: [Drawable] = [Circle(), Circle()] for shape in shapes { shape.draw() }
This program defines a Vehicle protocol. Both Car and Bike follow it. The function startJourney accepts any Vehicle and calls its drive method.
protocol Vehicle { func drive() } struct Car: Vehicle { func drive() { print("Car is driving") } } struct Bike: Vehicle { func drive() { print("Bike is driving") } } func startJourney(with vehicle: Vehicle) { vehicle.drive() } let myCar = Car() let myBike = Bike() startJourney(with: myCar) startJourney(with: myBike)
Protocols as types let you write code that works with many kinds of objects.
Only the methods and properties in the protocol are accessible when using the protocol as a type.
If you need to access something specific to a concrete type, you must cast it.
Protocols define a set of features or rules.
Using a protocol as a type means any object following those rules can be used.
This makes your code flexible and easier to maintain.
Practice
protocol as a type in Swift?Solution
Step 1: Understand what a protocol defines
A protocol defines a set of rules or features that a type must implement.Step 2: Using protocol as a type
When a protocol is used as a type, any object that implements those rules can be assigned to that type.Final Answer:
Any object that follows the protocol's rules can be used as that type. -> Option AQuick Check:
Protocol as type means flexible object use = A [OK]
- Thinking protocols only apply to classes
- Believing protocols define stored properties
- Confusing protocol usage with inheritance
Drawable?Solution
Step 1: Understand protocol type declaration
To declare a variable of a protocol type, use the protocol name without parentheses or brackets.Step 2: Analyze options
var shape: Drawable correctly declares a variable of type Drawable. Options A and B incorrectly try to instantiate a protocol, which is not allowed. var shape: Drawable[] uses invalid syntax.Final Answer:
var shape: Drawable -> Option AQuick Check:
Protocol type variable = var name: ProtocolName [OK]
- Trying to instantiate a protocol directly
- Using parentheses after protocol name
- Using array brackets incorrectly
protocol Greetable {
func greet() -> String
}
struct Person: Greetable {
func greet() -> String {
"Hello"
}
}
let greeter: Greetable = Person()
print(greeter.greet())Solution
Step 1: Understand protocol conformance
Person struct conforms to Greetable and implements greet() returning "Hello".Step 2: Using protocol as type and calling method
Variable greeter is of type Greetable but holds a Person instance, so calling greet() returns "Hello".Final Answer:
Hello -> Option DQuick Check:
Protocol method call returns implemented string = Hello [OK]
- Expecting protocol name or method name as output
- Thinking protocol cannot be used as variable type
- Confusing compile error with runtime behavior
protocol Vehicle {
func start()
}
class Car: Vehicle {
func start() {
print("Car started")
}
}
var myVehicle: Vehicle = Vehicle()
myVehicle.start()Solution
Step 1: Check protocol instantiation
Protocols cannot be instantiated directly, so Vehicle() causes an error.Step 2: Analyze variable assignment
Variable myVehicle must hold an instance of a type conforming to Vehicle, like Car(), not Vehicle() itself.Final Answer:
Cannot instantiate protocol directly with Vehicle() -> Option CQuick Check:
Protocols can't be created with () = Error [OK]
- Trying to create an instance of a protocol
- Confusing protocol with class instantiation
- Ignoring method return types
Animal in an array and calls sound() on each?protocol Animal {
func sound() -> String
}
class Dog: Animal {
func sound() -> String { "Bark" }
}
class Cat: Animal {
func sound() -> String { "Meow" }
}Solution
Step 1: Understand protocol array usage
To store different types conforming to Animal, declare array as [Animal].Step 2: Call method correctly
Use animal.sound() with parentheses to call the method. let animals: [Animal] = [Dog(), Cat()] for animal in animals { print(animal.sound()) } does this correctly.Final Answer:
let animals: [Animal] = [Dog(), Cat()] for animal in animals { print(animal.sound()) } -> Option BQuick Check:
Protocol array with method calls = let animals: [Animal] = [Dog(), Cat()] for animal in animals { print(animal.sound()) } [OK]
- Using concrete type array instead of protocol type
- Calling method without parentheses
- Mixing incompatible types in array
Ready to prove it?
Pass the test and this pattern moves unknown
