Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a protocol named Drivable with a drive() method.
iOS Swift
protocol Drivable {
func [1]()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name different from the protocol's intended method.
Forgetting parentheses after the method name.
✗ Incorrect
The protocol requires a method named
drive() to be implemented by conforming types.2fill in blank
mediumComplete the code to make Car conform to the Drivable protocol by implementing the drive() method.
iOS Swift
struct Car: Drivable {
func [1]() {
print("Car is driving")
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the protocol requires.
Missing the method implementation.
✗ Incorrect
The
Car struct must implement the drive() method to conform to Drivable.3fill in blank
hardFix the error in the protocol extension by completing the method name to provide a default implementation for drive().
iOS Swift
extension Drivable {
func [1]() {
print("Default driving behavior")
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the extension.
Not matching the protocol method signature.
✗ Incorrect
The protocol extension provides a default implementation for the
drive() method.4fill in blank
hardFill both blanks to declare a protocol Flyable with a method fly() and make Airplane conform to it.
iOS Swift
protocol [1] { func fly() } struct Airplane: [2] { func fly() { print("Airplane is flying") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the protocol and conformance.
Forgetting to implement the required method.
✗ Incorrect
The protocol is named
Flyable and the struct Airplane conforms to it by implementing fly().5fill in blank
hardFill all three blanks to create a protocol Vehicle with a property speed and a method move(), then make Bike conform to it.
iOS Swift
protocol [1] { var [2]: Int { get set } func [3]() } struct Bike: Vehicle { var speed: Int = 10 func move() { print("Bike is moving at \(speed) km/h") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property or method names.
Not marking the property with { get set } in the protocol.
✗ Incorrect
The protocol
Vehicle requires a speed property and a move() method. Bike conforms by implementing both.