0
0
iOS Swiftmobile~20 mins

Protocol-oriented architecture 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
2:00remaining
Understanding Protocol Extensions
What will be printed when the following Swift code runs?
iOS Swift
protocol Greetable {
  func greet() -> String
}

extension Greetable {
  func greet() -> String {
    return "Hello from protocol extension"
  }
}

struct Person: Greetable {}

let p = Person()
print(p.greet())
AHello from protocol extension
BError: Protocol method not implemented
CHello from Person struct
DRuntime crash
Attempts:
2 left
💡 Hint
Protocol extensions provide default implementations.
ui_behavior
intermediate
2:00remaining
Protocol and UIViewController Behavior
Given a protocol with a default method to setup UI, which option correctly applies the protocol to a UIViewController subclass and calls the setup method?
iOS Swift
protocol UISetup {
  func setupUI()
}

extension UISetup {
  func setupUI() {
    print("Default UI setup")
  }
}

class MyViewController: UIViewController, UISetup {
  override func viewDidLoad() {
    super.viewDidLoad()
    // Call setupUI here
  }
}
Aoverride func viewDidLoad() { super.viewDidLoad(); self.setupUI }
Boverride func viewDidLoad() { super.viewDidLoad(); self.UISetup.setupUI() }
Coverride func viewDidLoad() { super.viewDidLoad(); setupUI() }
Doverride func viewDidLoad() { super.viewDidLoad(); UISetup.setupUI() }
Attempts:
2 left
💡 Hint
Call the protocol method directly on self.
lifecycle
advanced
2:00remaining
Protocol-oriented Initialization
What error occurs when trying to compile this Swift code?
iOS Swift
protocol Identifiable {
  var id: String { get }
  init(id: String)
}

struct User: Identifiable {
  let id: String
  init(id: String) {
    self.id = id
  }
}

let user = User(id: "123")
AError: Type 'User' does not conform to protocol 'Identifiable' because init(id:) is missing
BNo error, compiles and runs fine
CError: Cannot use init in protocol
DRuntime error: Missing initializer
Attempts:
2 left
💡 Hint
Structs do not inherit protocol initializers automatically.
navigation
advanced
2:00remaining
Protocol and Navigation Controller
Which option correctly uses a protocol to define a navigation action and implements it in a UIViewController subclass?
iOS Swift
protocol Navigatable {
  func navigateToDetail()
}

class ListViewController: UIViewController, Navigatable {
  func navigateToDetail() {
    let detailVC = UIViewController()
    // Navigate to detailVC
  }
}
AnavigationController?.pushViewController(detailVC, animated: true)
Bself.show(detailVC)
Cpresent(detailVC, animated: true)
DdetailVC.navigationController?.pushViewController(self, animated: true)
Attempts:
2 left
💡 Hint
Use navigationController to push new view controller.
📝 Syntax
expert
2:00remaining
Protocol Composition and Method Resolution
What is the output of this Swift code?
iOS Swift
protocol A {
  func foo() -> String
}

protocol B {
  func foo() -> String
}

extension A {
  func foo() -> String { "From A" }
}

extension B {
  func foo() -> String { "From B" }
}

struct C: A, B {}

let c = C()
print(c.foo())
AFrom A
BFrom B
CRuntime crash
DError: Ambiguous use of 'foo()'
Attempts:
2 left
💡 Hint
Multiple protocol extensions provide the same method.