📖 Scenario: Imagine you are creating a simple app that manages different types of vehicles. You want to make sure all vehicles can start and stop, but each vehicle might do it differently.
🎯 Goal: You will create a Vehicle protocol that requires two functions: start() and stop(). Then, you will create a struct that follows this protocol.
📋 What You'll Learn
Create a protocol named Vehicle
Add two function declarations inside the protocol: start() and stop()
Create a struct named Car that conforms to the Vehicle protocol
Implement the start() and stop() functions inside Car
Print messages when start() and stop() are called
💡 Why This Matters
🌍 Real World
Protocols are used in apps to define common behaviors for different types, like vehicles, animals, or user interface elements.
💼 Career
Understanding protocols is essential for Swift developers to write flexible and reusable code, especially in app development for iOS and macOS.
Progress0 / 4 steps
1
Create the Vehicle protocol
Write a protocol named Vehicle with two function declarations: start() and stop(). Do not add any code inside the functions.
Swift
Hint
Use the protocol keyword followed by the protocol name. Inside curly braces, declare the functions without bodies.
2
Create the Car struct conforming to Vehicle
Create a struct named Car that conforms to the Vehicle protocol.
Swift
Hint
Use struct Car: Vehicle to declare the struct and protocol conformance.
3
Implement start() and stop() in Car
Inside the Car struct, implement the start() function to print "Car started" and the stop() function to print "Car stopped".
Swift
Hint
Use print("Car started") inside start() and print("Car stopped") inside stop().
4
Create a Car instance and call start() and stop()
Create a variable named myCar as an instance of Car. Then call myCar.start() and myCar.stop() to print the messages.
Swift
Hint
Create myCar with let myCar = Car(). Then call myCar.start() and myCar.stop().
Practice
(1/5)
1. What keyword is used in Swift to declare a protocol?
easy
A. interface
B. class
C. protocol
D. struct
Solution
Step 1: Recall Swift syntax for protocols
In Swift, protocols are declared using the keyword protocol.
Step 2: Compare options with known syntax
Options like interface are from other languages, and class or struct declare types, not protocols.
Final Answer:
protocol -> Option C
Quick Check:
Keyword for protocol declaration = protocol [OK]
Hint: Remember: Swift uses 'protocol' keyword for protocols [OK]
Common Mistakes:
Confusing 'protocol' with 'interface' from other languages
Using 'class' or 'struct' instead of 'protocol'
Misspelling the keyword
2. Which of the following is the correct syntax to declare a protocol named Vehicle in Swift?
easy
A. protocol Vehicle {}
B. protocol Vehicle() {}
C. protocol Vehicle[] {}
D. protocol Vehicle<> {}
Solution
Step 1: Understand protocol declaration syntax
Protocols are declared with the keyword protocol, 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 A
Quick Check:
Protocol declaration uses curly braces only [OK]
Hint: Protocols use curly braces, no parentheses or brackets [OK]
Hint: Always add { get } or { get set } to protocol properties [OK]
Common Mistakes:
Omitting { get } or { get set } in protocol properties
Adding parentheses to property declarations
Thinking protocol names must be uppercase
5. You want to declare a protocol Identifiable that requires a read-only id property of type String and a method identify() that returns a String. Which is the correct declaration?
hard
A. protocol Identifiable {
let id: String
func identify() -> String
}
B. protocol Identifiable {
var id: String
func identify()
}
C. protocol Identifiable {
var id: String { get set }
func identify() -> String
}
D. protocol Identifiable {
var id: String { get }
func identify() -> String
}
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 method identify() must specify return type String as required.
Step 3: Evaluate options
protocol Identifiable {
var id: String { get }
func identify() -> String
} correctly declares var id: String { get } and func identify() -> String. protocol Identifiable {
var id: String
func identify()
} misses { get } and return type. protocol Identifiable {
let id: String
func identify() -> String
} uses let which 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 D
Quick Check:
Read-only property uses { get } and method return type matches [OK]
Hint: Use { get } for read-only properties in protocols [OK]
Common Mistakes:
Using 'let' instead of 'var' in protocol
Omitting { get } for read-only properties
Missing return type in method declaration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.