Protocol-oriented architecture helps organize code by defining clear roles and behaviors. It makes your app easier to understand and change.
Protocol-oriented architecture in iOS Swift
protocol SomeProtocol {
func doSomething()
}
struct SomeStruct: SomeProtocol {
func doSomething() {
print("Doing something")
}
}A protocol defines a blueprint of methods or properties.
Types like structs, classes, or enums can conform to protocols by implementing the required methods.
Drivable and a struct Car that follows it.protocol Drivable {
func drive()
}
struct Car: Drivable {
func drive() {
print("Car is driving")
}
}Bird conforms to the Flyable protocol by implementing fly().protocol Flyable {
func fly()
}
class Bird: Flyable {
func fly() {
print("Bird is flying")
}
}protocol Identifiable {
var id: String { get }
}
struct User: Identifiable {
var id: String
}This program defines a protocol Greetable with a greet method. The struct Person follows the protocol and prints a greeting with the person's name.
protocol Greetable {
func greet()
}
struct Person: Greetable {
var name: String
func greet() {
print("Hello, my name is \(name)")
}
}
let person = Person(name: "Alex")
person.greet()Protocols help you write code that focuses on what to do, not how to do it.
Using protocols makes your code easier to change and test.
Swift encourages protocol-oriented programming as a modern way to design apps.
Protocols define a set of rules or behaviors.
Types conform to protocols by implementing required methods or properties.
Protocol-oriented architecture helps build flexible and reusable code.
Practice
protocol in Swift's protocol-oriented architecture?Solution
Step 1: Understand what a protocol is
A protocol defines a set of rules or requirements for methods and properties that any conforming type must implement.Step 2: Differentiate from other concepts
Protocols do not implement functionality themselves but specify what must be implemented, unlike classes or structs.Final Answer:
To define a blueprint of methods and properties that conforming types must implement -> Option BQuick Check:
Protocol = Blueprint for behavior [OK]
- Confusing protocols with classes or structs
- Thinking protocols store data
- Assuming protocols handle UI layout
Drivable with a method drive()?Solution
Step 1: Recall protocol declaration syntax
Protocols are declared with the keywordprotocolfollowed by the protocol name and curly braces containing method signatures.Step 2: Identify correct method declaration
The methoddrive()is declared without a body inside the protocol.Final Answer:
protocol Drivable { func drive() } -> Option DQuick Check:
Protocol syntax = protocol + name + method signatures [OK]
- Using class or struct instead of protocol keyword
- Adding method bodies inside protocol
- Incorrect function declaration syntax
car.start() is called?protocol Startable {
func start()
}
struct Car: Startable {
func start() {
print("Car started")
}
}
let car = Car()
car.start()Solution
Step 1: Check protocol conformance
The structCarconforms toStartableand implements the requiredstart()method.Step 2: Analyze method call output
Callingcar.start()executes the method which prints "Car started" to the console.Final Answer:
Car started -> Option AQuick Check:
Implemented method prints output [OK]
- Assuming missing implementation error
- Thinking protocol methods print automatically
- Confusing compilation errors with runtime
protocol Runnable {
func run()
}
struct Athlete: Runnable {
func run(distance: Int) {
print("Running \(distance) meters")
}
}Solution
Step 1: Compare protocol and struct method signatures
The protocol requiresfunc run()with no parameters, butAthleteimplementsfunc run(distance: Int)which has a parameter.Step 2: Understand conformance rules
Method signatures must match exactly for conformance; differing parameters cause a mismatch error.Final Answer:
Method signature in Athlete does not match protocol requirement -> Option CQuick Check:
Method signatures must match exactly [OK]
- Adding parameters to protocol methods in implementation
- Thinking structs can't conform to protocols
- Ignoring method signature differences
play() method, and some can also be "recordable" with a record() method. Which protocol-oriented approach below best supports this design?Solution
Step 1: Understand protocol composition
Defining separate protocols allows types to conform only to behaviors they support, making the design flexible and reusable.Step 2: Evaluate other options
Single protocol forces all types to implement all methods, base class limits flexibility, and global functions do not enforce behavior contracts.Final Answer:
Define separate protocols Playable and Recordable, then have types conform to one or both as needed -> Option AQuick Check:
Separate protocols = flexible conformance [OK]
- Forcing all types to implement all methods
- Using inheritance instead of protocols
- Not leveraging protocol composition
Ready to prove it?
Pass the test and this pattern moves unknown
