Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a protocol named Drivable.
iOS Swift
protocol [1] {
func drive()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the protocol name.
Using a verb form instead of an adjective.
✗ Incorrect
The protocol name should be Drivable to match the task requirement.
2fill in blank
mediumComplete the code to make the class Car conform to the Drivable protocol.
iOS Swift
class Car: [1] { func drive() { print("Driving the car") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong protocol name.
Forgetting to conform to the protocol.
✗ Incorrect
The class must conform to the Drivable protocol to implement its requirements.
3fill in blank
hardFix the error in the protocol method declaration to make it optional.
iOS Swift
@objc protocol Drivable {
[1] func drive()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'optional' keyword alone.
Missing the @objc attribute on the protocol.
✗ Incorrect
In Swift, to make a protocol method optional, the protocol must be marked @objc and the method marked @objc optional.
4fill in blank
hardFill both blanks to declare a protocol Playable with a property and a method requirement.
iOS Swift
protocol Playable {
var isPlaying: [1] { get set }
func [2]()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property type.
Using incorrect method name.
✗ Incorrect
The property 'isPlaying' should be of type Bool, and the method name should be 'play' to match the protocol requirements.
5fill in blank
hardFill all three blanks to implement the Playable protocol in class MusicPlayer.
iOS Swift
class MusicPlayer: Playable { var isPlaying: [1] = false func [2]() { isPlaying = true print("Music started") } func [3]() { isPlaying = false print("Music stopped") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property type.
Incorrect method names like 'start' instead of 'play'.
✗ Incorrect
The property 'isPlaying' is Bool. The 'play' method fulfills the protocol requirement, and 'stop' is an additional method.