Protocols define a set of rules that classes, structs, or enums must follow. They make sure certain methods and properties are present.
Protocol requirements (methods and properties) in Swift
protocol SomeProtocol { var mustHaveProperty: Int { get set } func requiredMethod() -> String }
Properties in protocols can be read-only ({ get }) or read-write ({ get set }).
Methods in protocols only declare the signature, not the body.
protocol Vehicle { var numberOfWheels: Int { get } func drive() }
protocol Person { var name: String { get set } func greet() -> String }
protocol Logger { func log(message: String) }
This program defines a protocol Animal with a property and a method. The struct Dog follows the protocol by implementing the property and method. It then prints the dog's name and sound.
protocol Animal { var name: String { get } func sound() -> String } struct Dog: Animal { var name: String func sound() -> String { return "Woof!" } } let myDog = Dog(name: "Buddy") print("\(myDog.name) says \(myDog.sound())")
When a type adopts a protocol, it must implement all required methods and properties.
Properties can be declared as { get } or { get set } to control if they are read-only or read-write.
Protocols help write code that is easier to test and maintain.
Protocols define required methods and properties without implementation.
Types that adopt protocols must implement all requirements.
Use protocols to create flexible and reusable code.
Practice
Solution
Step 1: Understand protocol purpose
Protocols define a set of methods and properties that a type must implement.Step 2: Check adoption requirements
Any type adopting a protocol must provide implementations for all its requirements.Final Answer:
To implement all the methods and properties declared in the protocol -> Option DQuick Check:
Protocol adoption = implement all requirements [OK]
- Thinking protocols require inheritance from a class
- Assuming methods can be skipped
- Confusing protocol adoption with subclassing
Solution
Step 1: Recall property syntax in protocols
Read-only properties use { get } to indicate they must be readable but not necessarily writable.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.Final Answer:
var name: String { get } -> Option AQuick Check:
Read-only property = { get } [OK]
- Using let instead of var in protocol
- Confusing methods with properties
- Using { get set } for read-only properties
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?
Solution
Step 1: Check protocol conformance
Car struct implements the required property speed and method description(), fulfilling Vehicle protocol.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".Final Answer:
Car speed is 80.0 km/h -> Option BQuick Check:
Protocol methods implemented = correct output [OK]
- Expecting 80 instead of 80.0
- Thinking protocol conformance causes error
- Missing string interpolation syntax
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?
Solution
Step 1: Check protocol requirements
Shape protocol requires a read-only property area and a method perimeter().Step 2: Verify Square struct implementation
Square implements area as computed property but does not implement perimeter() method.Final Answer:
Missing implementation of perimeter() method -> Option CQuick Check:
All protocol methods must be implemented [OK]
- Assuming computed properties are not allowed
- Thinking get set is required for read-only
- Ignoring missing method implementations
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()?Solution
Step 1: Define protocol requirements
id must be read-only, so use { get } and identify() method with no return.Step 2: Provide default implementation in extension
Extension implements identify() printing "ID: \(id)" matching protocol signature.Final Answer:
protocol Identifiable { var id: String { get }; func identify() } extension Identifiable { func identify() { print("ID: \(id)") } } -> Option AQuick Check:
Read-only property + default method in extension [OK]
- Using let in protocol property
- Changing method signature in extension
- Making id writable when only read-only needed
Ready to prove it?
Pass the test and this pattern moves unknown
