Bird
0
0

Examine this Swift code:

medium📝 Debug Q6 of 15
iOS Swift - Swift Language Essentials
Examine this Swift code:
protocol Drawable {
  func draw()
}

class Shape: Drawable {
  func draw(color: String) {
    print("Drawing with color \(color)")
  }
}

What is the issue with the Shape class implementation?
AThe class <code>Shape</code> should be declared as a struct.
BThe method signature in <code>Shape</code> does not match the protocol requirement.
CThe protocol <code>Drawable</code> cannot be adopted by classes.
DThe method <code>draw()</code> should have a return type.
Step-by-Step Solution
Solution:
  1. Step 1: Check protocol method signature

    The protocol Drawable requires a method draw() with no parameters.
  2. Step 2: Compare with class method

    The class Shape implements draw(color: String), which has a parameter and thus does not match the protocol requirement.
  3. Final Answer:

    The method signature mismatch causes a protocol conformance error. -> Option B
  4. Quick Check:

    Method signatures must match exactly [OK]
Quick Trick: Method signatures must exactly match protocol requirements [OK]
Common Mistakes:
  • Assuming parameter names can differ
  • Thinking classes can't adopt protocols
  • Ignoring method parameters in protocol conformance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes