0
0
iOS Swiftmobile~10 mins

Protocol-oriented architecture in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Arun
Bstart
Cdrive
Dmove
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name different from the protocol's intended method.
Forgetting parentheses after the method name.
2fill in blank
medium

Complete 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'
Amove
Bdrive
Cstart
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the protocol requires.
Missing the method implementation.
3fill in blank
hard

Fix 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'
Amove
Brun
Cstart
Ddrive
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the extension.
Not matching the protocol method signature.
4fill in blank
hard

Fill 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'
AFlyable
BDrivable
DRunnable
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the protocol and conformance.
Forgetting to implement the required method.
5fill in blank
hard

Fill 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'
AVehicle
Bspeed
Cmove
Ddrive
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property or method names.
Not marking the property with { get set } in the protocol.