0
0
iOS Swiftmobile~20 mins

Protocols in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Protocol Requirements
Given the protocol and struct below, what will be the output when calling print(user.description)?
iOS Swift
protocol Describable {
  var description: String { get }
}

struct User: Describable {
  var name: String
  var description: String {
    return "User: \(name)"
  }
}

let user = User(name: "Alice")
print(user.description)
AAlice
BUser
CUser: Alice
DCompilation error: missing protocol requirement
Attempts:
2 left
💡 Hint
Check how the description property is implemented in the struct.
ui_behavior
intermediate
1:30remaining
Protocol and UIViewController Behavior
If a UIViewController conforms to a protocol with a method setupUI() and calls it inside viewDidLoad(), what happens when the view loads?
iOS Swift
protocol UISetup {
  func setupUI()
}

class MyViewController: UIViewController, UISetup {
  override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
  }
  func setupUI() {
    view.backgroundColor = .blue
  }
}
ACompilation error: UIViewController cannot conform to protocols.
BThe app crashes because setupUI() is not called.
CThe view background color remains default (white).
DThe view background color becomes blue when the view loads.
Attempts:
2 left
💡 Hint
Look at when setupUI() is called and what it does.
lifecycle
advanced
2:00remaining
Protocol Extension and Method Dispatch
What will be printed when running the code below?
iOS Swift
protocol Greetable {
  func greet()
}

extension Greetable {
  func greet() {
    print("Hello from protocol extension")
  }
}

class Person: Greetable {
  func greet() {
    print("Hello from Person class")
  }
}

let p: Greetable = Person()
p.greet()
AHello from Person class
BHello from protocol extension
CCompilation error: greet() ambiguous
DRuntime error: method not found
Attempts:
2 left
💡 Hint
Consider how Swift chooses methods when a class implements a protocol method and the protocol has a default implementation.
📝 Syntax
advanced
2:00remaining
Protocol with Associated Type Syntax
Which option correctly declares a protocol with an associated type and a method using it?
Aprotocol Container { associatedtype ItemType func append(_ item: ItemType) }
Bprotocol Container { typealias ItemType func append(_ item: ItemType) }
Cprotocol Container { associatedtype ItemType func append(item: ItemType) -> Void }
Dprotocol Container { associatedtype ItemType func append(item: ItemType) }
Attempts:
2 left
💡 Hint
Look for the correct keyword to declare an associated type and method parameter syntax.
🔧 Debug
expert
2:30remaining
Resolving Protocol Conformance Ambiguity
Given the code below, what error will the compiler produce?
iOS Swift
protocol Drawable {
  func draw()
}

extension Drawable {
  func draw() {
    print("Drawing from extension")
  }
}

class Shape: Drawable {
}

let shape = Shape()
shape.draw()
AError: Type 'Shape' does not conform to protocol 'Drawable' because 'draw()' is not implemented
BNo error; prints "Drawing from extension"
CError: Ambiguous use of 'draw()'
DRuntime error: method 'draw()' not found
Attempts:
2 left
💡 Hint
Check if protocol extension provides a default implementation and if the class must implement it explicitly.