📖 Scenario: You are building a simple app that manages different types of vehicles. Each vehicle can start its engine, but the way it starts might differ. You want to use a protocol to represent any vehicle type and treat them uniformly.
🎯 Goal: Create a protocol called Vehicle with a startEngine() method. Then create two structs, Car and Motorcycle, that conform to Vehicle. Finally, use the protocol as a type to store different vehicles in an array and start their engines.
📋 What You'll Learn
Create a protocol named Vehicle with a method startEngine() that returns a String.
Create a struct Car that conforms to Vehicle and implements startEngine() returning "Car engine started".
Create a struct Motorcycle that conforms to Vehicle and implements startEngine() returning "Motorcycle engine started".
Create an array called vehicles of type [Vehicle] containing one Car and one Motorcycle.
Use a for loop to call startEngine() on each vehicle in vehicles and print the result.
💡 Why This Matters
🌍 Real World
Protocols let you write flexible code that can work with many types sharing the same behavior, like different vehicles in a transport app.
💼 Career
Understanding protocols and using them as types is key for Swift developers to write clean, reusable, and scalable code.
Progress0 / 4 steps
1
Create the Vehicle protocol
Create a protocol called Vehicle with a method startEngine() that returns a String.
Swift
Hint
Use the protocol keyword and define startEngine() as a function returning String.
2
Create Car and Motorcycle structs conforming to Vehicle
Create a struct called Car that conforms to Vehicle and implements startEngine() returning "Car engine started". Also create a struct called Motorcycle that conforms to Vehicle and implements startEngine() returning "Motorcycle engine started".
Swift
Hint
Define structs with struct Name: Vehicle and implement startEngine() returning the exact strings.
3
Create an array of Vehicle type
Create an array called vehicles of type [Vehicle] containing one Car() and one Motorcycle().
Swift
Hint
Use let vehicles: [Vehicle] = [Car(), Motorcycle()] to create the array.
4
Loop through vehicles and print engine start messages
Use a for loop with variable vehicle to iterate over vehicles. Inside the loop, call startEngine() on vehicle and print the returned string.
Swift
Hint
Use for vehicle in vehicles and print(vehicle.startEngine()).
Practice
(1/5)
1. What does it mean to use a protocol as a type in Swift?
easy
A. Any object that follows the protocol's rules can be used as that type.
B. Only classes can conform to the protocol.
C. Protocols can only be used to create new classes.
D. Protocols define stored properties for types.
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 A
Quick Check:
Protocol as type means flexible object use = A [OK]
Hint: Protocol as type means any conforming object fits [OK]
Common Mistakes:
Thinking protocols only apply to classes
Believing protocols define stored properties
Confusing protocol usage with inheritance
2. Which of the following is the correct way to declare a variable of a protocol type named Drawable?
easy
A. var shape: Drawable
B. var shape: Drawable()
C. var shape: Drawable = Drawable()
D. var shape: 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 A
Quick Check:
Protocol type variable = var name: ProtocolName [OK]
Hint: Declare protocol type without parentheses or initialization [OK]
A. let animals: [Animal] = [Dog(), Cat()]
for animal in animals {
print(animal.sound)
}
B. let animals: [Animal] = [Dog(), Cat()]
for animal in animals {
print(animal.sound())
}
C. let animals: [Dog] = [Dog(), Cat()]
for animal in animals {
print(animal.sound())
}
D. let animals = [Dog(), Cat()]
for animal in animals {
print(animal.sound())
}
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 B
Quick Check:
Protocol array with method calls = let animals: [Animal] = [Dog(), Cat()]
for animal in animals {
print(animal.sound())
} [OK]
Hint: Use protocol array type and call methods with () [OK]
Common Mistakes:
Using concrete type array instead of protocol type
Calling method without parentheses
Mixing incompatible types in array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.