Bird
0
0

You want to create a protocol Vehicle with a property speed that can be read and set. Which of these protocol declarations is correct?

hard📝 Application Q15 of 15
iOS Swift - Swift Language Essentials
You want to create a protocol Vehicle with a property speed that can be read and set. Which of these protocol declarations is correct?
Aprotocol Vehicle { var speed: Int { get set } }
Bprotocol Vehicle { var speed: Int }
Cprotocol Vehicle { func speed() -> Int }
Dprotocol Vehicle { let speed: Int }
Step-by-Step Solution
Solution:
  1. Step 1: Understand property requirements in protocols

    To require a property to be readable and writable, use { get set } in the protocol.
  2. Step 2: Analyze each option

    protocol Vehicle { var speed: Int { get set } } correctly declares speed as a variable property with get set. protocol Vehicle { var speed: Int } misses get set, so it's read-only. protocol Vehicle { func speed() -> Int } declares a method, not a property. protocol Vehicle { let speed: Int } uses let, which is not allowed in protocols.
  3. Final Answer:

    protocol Vehicle { var speed: Int { get set } } -> Option A
  4. Quick Check:

    Writable property = var with get set [OK]
Quick Trick: Use 'var' with { get set } for read-write properties [OK]
Common Mistakes:
  • Using 'let' in protocol properties
  • Omitting { get set } for writable properties
  • Confusing methods with properties

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes