Bird
Raised Fist0
Swiftprogramming~10 mins

Protocol requirements (methods and properties) in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Protocol requirements (methods and properties)
Define Protocol
↓
Declare Method & Property Requirements
↓
Create Struct/Class
↓
Implement Required Methods & Properties
↓
Use Instance of Struct/Class
↓
Call Methods & Access Properties
↓
Program Runs Successfully
This flow shows how you define a protocol with required methods and properties, then create a type that implements them, and finally use that type.
Execution Sample
Swift
protocol Vehicle {
    var speed: Int { get set }
    mutating func accelerate()
}

struct Car: Vehicle {
    var speed = 0
    mutating func accelerate() { speed += 10 }
}
Defines a protocol Vehicle with a property and method requirement, then a struct Car that implements them.
Execution Table
StepActionVariable/PropertyValueNote
1Define protocol Vehicle--Protocol requires speed property and accelerate() method
2Create Car instancecar.speed0Initial speed is 0
3Call car.accelerate()car.speed10Speed increases by 10
4Call car.accelerate() againcar.speed20Speed increases by 10 again
5End--Program ends after method calls
💡 No more method calls, execution ends
Variable Tracker
Variable/PropertyStartAfter 1 accelerate()After 2 accelerate() callsFinal
car.speed0102020
Key Moments - 2 Insights
Why must Car implement both the speed property and accelerate() method?
Because the protocol Vehicle requires both. The execution_table step 1 shows these requirements, and step 2 fails if Car does not implement them.
Why does speed change when calling accelerate()?
Because accelerate() method increases speed by 10 each time, as shown in execution_table steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is car.speed after the first accelerate() call?
A10
B0
C20
DUndefined
💡 Hint
Check execution_table row 3 where car.speed changes to 10
At which step does the protocol requirement get fulfilled by Car?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Step 2 shows Car instance created with speed property implemented
If accelerate() added 5 instead of 10, what would car.speed be after two calls?
A10
B5
C15
D20
💡 Hint
Each call adds 5, so after two calls speed is 5 + 5 = 15
Concept Snapshot
protocol ProtocolName {
  var propertyName: Type { get set }
  func methodName()
}

struct/ class TypeName: ProtocolName {
  var propertyName = initialValue
  func methodName() { /* implementation */ }
}

- Protocol defines required methods and properties.
- Types must implement all requirements.
- Properties can be gettable or get/set.
- Methods define behavior to implement.
Full Transcript
This example shows how to define a protocol in Swift with required properties and methods. The protocol Vehicle requires a speed property and an accelerate() method. The struct Car implements these requirements by defining speed and the accelerate() method that increases speed. The execution table traces creating a Car instance, calling accelerate twice, and how speed changes from 0 to 10 then 20. Key moments clarify why Car must implement all protocol requirements and how method calls change property values. The visual quiz tests understanding of property values after method calls and protocol fulfillment steps.

Practice

(1/5)
1. What does a protocol in Swift require from a type that adopts it?
easy
A. To declare new methods unrelated to the protocol
B. To inherit from a superclass
C. To override all existing methods
D. To implement all the methods and properties declared in the protocol

Solution

  1. Step 1: Understand protocol purpose

    Protocols define a set of methods and properties that a type must implement.
  2. Step 2: Check adoption requirements

    Any type adopting a protocol must provide implementations for all its requirements.
  3. Final Answer:

    To implement all the methods and properties declared in the protocol -> Option D
  4. Quick Check:

    Protocol adoption = implement all requirements [OK]
Hint: Protocols require full implementation of declared methods and properties [OK]
Common Mistakes:
  • Thinking protocols require inheritance from a class
  • Assuming methods can be skipped
  • Confusing protocol adoption with subclassing
2. Which of the following is the correct way to declare a read-only property requirement in a Swift protocol?
easy
A. var name: String { get }
B. var name: String { get set }
C. let name: String
D. func name() -> String

Solution

  1. Step 1: Recall property syntax in protocols

    Read-only properties use { get } to indicate they must be readable but not necessarily writable.
  2. Step 2: Analyze options

    var name: String { get } uses { get } correctly; var name: String { get set } requires read-write; let name: String is invalid in protocols; func name() -> String is a method, not a property.
  3. Final Answer:

    var name: String { get } -> Option A
  4. Quick Check:

    Read-only property = { get } [OK]
Hint: Use { get } for read-only property in protocols [OK]
Common Mistakes:
  • Using let instead of var in protocol
  • Confusing methods with properties
  • Using { get set } for read-only properties
3. Consider the following Swift code:
protocol Vehicle {
    var speed: Double { get set }
    func description() -> String
}

struct Car: Vehicle {
    var speed: Double
    func description() -> String {
        return "Car speed is \(speed) km/h"
    }
}

let myCar = Car(speed: 80)
print(myCar.description())

What is the output when this code runs?
medium
A. Error: Protocol requirements not met
B. Car speed is 80.0 km/h
C. Car speed is 80 km/h
D. Car speed is km/h

Solution

  1. Step 1: Check protocol conformance

    Car struct implements the required property speed and method description(), fulfilling Vehicle protocol.
  2. Step 2: Analyze print output

    The description() returns "Car speed is \(speed) km/h". Since speed is 80.0 (Double), string interpolation formats it as "80.0".
  3. Final Answer:

    Car speed is 80.0 km/h -> Option B
  4. Quick Check:

    Protocol methods implemented = correct output [OK]
Hint: String interpolation for Double 80.0 prints '80.0' [OK]
Common Mistakes:
  • Expecting 80 instead of 80.0
  • Thinking protocol conformance causes error
  • Missing string interpolation syntax
4. Given this protocol and struct:
protocol Shape {
    var area: Double { get }
    func perimeter() -> Double
}

struct Square: Shape {
    var side: Double
    var area: Double {
        side * side
    }
}

What is the error in the code?
medium
A. area property should be var with get set
B. side property must be a constant (let)
C. Missing implementation of perimeter() method
D. Square cannot have computed properties

Solution

  1. Step 1: Check protocol requirements

    Shape protocol requires a read-only property area and a method perimeter().
  2. Step 2: Verify Square struct implementation

    Square implements area as computed property but does not implement perimeter() method.
  3. Final Answer:

    Missing implementation of perimeter() method -> Option C
  4. Quick Check:

    All protocol methods must be implemented [OK]
Hint: All protocol methods must be implemented, or error occurs [OK]
Common Mistakes:
  • Assuming computed properties are not allowed
  • Thinking get set is required for read-only
  • Ignoring missing method implementations
5. You want to create a protocol Identifiable that requires a read-only id property of type String and a method identify() that prints the id. Which of the following is the correct protocol and extension to provide a default implementation of identify()?
hard
A. protocol Identifiable { var id: String { get } func identify() } extension Identifiable { func identify() { print("ID: \(id)") } }
B. protocol Identifiable { var id: String { get set } func identify() } extension Identifiable { func identify() { print(id) } }
C. protocol Identifiable { let id: String func identify() } extension Identifiable { func identify() { print("ID is " + id) } }
D. protocol Identifiable { var id: String { get } func identify() -> String } extension Identifiable { func identify() -> String { return id } }

Solution

  1. Step 1: Define protocol requirements

    id must be read-only, so use { get } and identify() method with no return.
  2. Step 2: Provide default implementation in extension

    Extension implements identify() printing "ID: \(id)" matching protocol signature.
  3. Final Answer:

    protocol Identifiable { var id: String { get }; func identify() } extension Identifiable { func identify() { print("ID: \(id)") } } -> Option A
  4. Quick Check:

    Read-only property + default method in extension [OK]
Hint: Use protocol extension for default method implementations [OK]
Common Mistakes:
  • Using let in protocol property
  • Changing method signature in extension
  • Making id writable when only read-only needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard