Junior — Middle
Is it possible to implement protocols in the program code?
sobes.tech AI
Answer from AI
Yes, protocols can be implemented in programming code. In the context of iOS and Swift, a protocol is a set of requirements (methods, properties) that a class, struct, or enumeration must implement.
Example of implementing a protocol in Swift:
protocol Drivable {
func drive()
}
class Car: Drivable {
func drive() {
print("The car is driving")
}
}
let myCar = Car()
myCar.drive() // Outputs: The car is driving
Thus, protocols define an interface, and specific types implement it, providing flexibility and extensibility of the code.