Bird
0
0

Which of the following is the correct syntax to declare a protocol named Playable with a method play()?

easy📝 Syntax Q3 of 15
iOS Swift - Swift Language Essentials
Which of the following is the correct syntax to declare a protocol named Playable with a method play()?
Aprotocol Playable { func play() -> Void }
Bprotocol Playable { func play }
Cprotocol Playable { func play() -> String }
Dprotocol Playable { var play: () -> Void }
Step-by-Step Solution
Solution:
  1. Step 1: Understand method declaration in protocols

    Methods in protocols are declared with their signature including return type. Void return type can be written as '-> Void' or omitted.
  2. Step 2: Check each option

    protocol Playable { func play() -> Void } correctly declares the method with explicit Void return type. protocol Playable { func play } is missing parentheses after 'play', resulting in a syntax error. protocol Playable { func play() -> String } specifies an incorrect String return type. protocol Playable { var play: () -> Void } declares a property with a closure type instead of a method.
  3. Final Answer:

    protocol Playable { func play() -> Void } -> Option A
  4. Quick Check:

    Protocol method syntax = func name() -> ReturnType [OK]
Quick Trick: Declare protocol methods with func and return type [OK]
Common Mistakes:
  • Using var instead of func
  • Omitting parentheses
  • Wrong return type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes