Swift Q & A

 

How do I create custom protocols in Swift?

Creating custom protocols in Swift is a fundamental aspect of protocol-oriented programming, allowing you to define a blueprint for methods, properties, and functionalities that can be adopted by classes, structs, or enums. Custom protocols enable you to establish a contract for conforming types, ensuring that they implement specific behaviors. Here’s a detailed guide on how to create custom protocols in Swift:

 

  1. Define a Protocol: To create a custom protocol, you start by using the `protocol` keyword, followed by the name of your protocol. Inside the protocol, you specify the methods, properties, or associated types that conforming types must implement. Here’s a basic example of a custom protocol called `Drawable`:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```swift
protocol Drawable {
func draw()
}
```
```swift protocol Drawable { func draw() } ```
```swift
protocol Drawable {
    func draw()
}
```
  1. Conforming to a Protocol: Once you’ve defined your custom protocol, you can make classes, structs, or enums conform to it by implementing its requirements. Conforming types must provide concrete implementations for all the methods and properties declared in the protocol. For instance:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```swift
struct Circle: Drawable {
func draw() {
// Implement drawing logic for a circle
}
}
```
```swift struct Circle: Drawable { func draw() { // Implement drawing logic for a circle } } ```
```swift
struct Circle: Drawable {
    func draw() {
        // Implement drawing logic for a circle
    }
}
```
  1. Multiple Protocol Conformance: Swift allows a type to conform to multiple protocols. This is a powerful feature that enables you to define more complex behaviors by combining multiple protocols. For example:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```swift
protocol Rotatable {
func rotate()
}
struct Square: Drawable, Rotatable {
func draw() {
// Implement drawing logic for a square
}
func rotate() {
// Implement rotation logic for a square
}
}
```
```swift protocol Rotatable { func rotate() } struct Square: Drawable, Rotatable { func draw() { // Implement drawing logic for a square } func rotate() { // Implement rotation logic for a square } } ```
```swift
protocol Rotatable {
    func rotate()
}

struct Square: Drawable, Rotatable {
    func draw() {
        // Implement drawing logic for a square
    }

    func rotate() {
        // Implement rotation logic for a square
    }
}
```
  1. Protocol Inheritance: Protocols in Swift can also inherit from other protocols. This allows you to build on existing protocols and define more specialized ones. For example:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```swift
protocol Printable {
func printDescription()
}
protocol DrawableAndPrintable: Drawable, Printable {
// Inherits the requirements from both Drawable and Printable
}
```
```swift protocol Printable { func printDescription() } protocol DrawableAndPrintable: Drawable, Printable { // Inherits the requirements from both Drawable and Printable } ```
```swift
protocol Printable {
    func printDescription()
}

protocol DrawableAndPrintable: Drawable, Printable {
    // Inherits the requirements from both Drawable and Printable
}
```
  1. Using Protocol Extensions: You can provide default implementations for methods in a protocol using protocol extensions. This allows you to offer a common implementation that conforming types can choose to override. 
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```swift
extension Drawable {
func draw() {
// Default implementation for drawing
}
}
```
```swift extension Drawable { func draw() { // Default implementation for drawing } } ```
```swift
extension Drawable {
    func draw() {
        // Default implementation for drawing
    }
}
```

In conclusion, creating custom protocols in Swift is a versatile way to define a contract for conforming types, enabling you to build modular and extensible code. Whether you need to establish common behavior for different types or create more specialized protocols, custom protocols are a fundamental concept in Swift’s protocol-oriented programming paradigm.

Previously at
Flag Argentina
Brazil
time icon
GMT-3
Experienced iOS Engineer with 7+ years mastering Swift. Created fintech solutions, enhanced biopharma apps, and transformed retail experiences.