0
0
iOS Swiftmobile~10 mins

Protocols 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.

iOS Swift
protocol [1] {
  func drive()
}
Drag options to blanks, or click blank then click option'
ADrivable
BDriveable
CDriving
DDrive
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the protocol name.
Using a verb form instead of an adjective.
2fill in blank
medium

Complete 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'
AVehicle
BDrivable
CDriveable
DRunnable
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong protocol name.
Forgetting to conform to the protocol.
3fill in blank
hard

Fix 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'
Aoptional
B@optional
C@objc optional
Doptional func
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'optional' keyword alone.
Missing the @objc attribute on the protocol.
4fill in blank
hard

Fill 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'
ABool
Bplay
CInt
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property type.
Using incorrect method name.
5fill in blank
hard

Fill 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'
ABool
Bplay
Cstop
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property type.
Incorrect method names like 'start' instead of 'play'.