Blog / March 5, 2023 / 3 mins read / By Mahi Garg

Protocols in Swift

In Swift, protocols are a powerful tool that empowers developers to create flexible, reusable, and scalable code. They play a pivotal role in achieving abstraction and enabling polymorphism, essential principles of object-oriented programming (OOP). In this blog, we’ll dive into the world of protocols in Swift, explore their features, benefits, and demonstrate how they can be used to design robust and adaptable code with practical examples.

x

What are Protocols?

Protocols in Swift are similar to interfaces in other programming languages. They define a blueprint of methods, properties, and other requirements that a class or struct must adopt. A class or struct that conforms to a protocol promises to provide the implementation for all the requirements defined in that protocol.

Declaring Protocols

Let’s start by creating a simple protocol that defines the requirements for an object that can make a sound:

protocol SoundMaker {
    func makeSound()
}

In this example, we’ve declared a protocol called SoundMaker with a single requirement: the makeSound() method.

Adopting Protocols

Now, let’s create two classes that adopt the SoundMaker protocol:

class Dog: SoundMaker {
    func makeSound() {
        print("Woof woof!")
    }
}

class Cat: SoundMaker {
    func makeSound() {
        print("Meow!")
    }
}

Both the Dog and Cat classes implement the makeSound() method as required by the SoundMaker protocol.

Using Protocols

Now that we have our classes conforming to the SoundMaker protocol, we can use the protocol type to create a collection of different sound-making objects:

let dog = Dog()
let cat = Cat()

let soundMakers: [SoundMaker] = [dog, cat]

for soundMaker in soundMakers {
    soundMaker.makeSound()
}

The output will be:

Woof woof!
Meow!

Protocol Properties

Protocols can also require properties to be implemented. Let’s modify our SoundMaker protocol to include a name property:

protocol SoundMaker {
    var name: String { get }
    func makeSound()
}

Now, the classes that adopt the SoundMaker protocol must provide an implementation for the name property:

class Dog: SoundMaker {
    let name: String

    init(name: String) {
        self.name = name
    }

    func makeSound() {
        print("Woof woof!")
    }
}

class Cat: SoundMaker {
    let name: String

    init(name: String) {
        self.name = name
    }

    func makeSound() {
        print("Meow!")
    }
}

Optional Protocol Requirements

In Swift, protocols can also have optional requirements. This means that conforming classes or structs may or may not implement those requirements. To define an optional requirement in a protocol, you use the @objc attribute and mark the requirement with the optional keyword:

@objc protocol OptionalSoundMaker {
    @objc optional func optionalSound()
}

Classes or structs that conform to this protocol can choose to implement the optionalSound() method or not. Note that protocols with optional requirements must be marked with @objc.

Conclusion

Protocols are a crucial feature in Swift for designing flexible and reusable code. By defining requirements that classes or structs must adopt, protocols enable polymorphism, allowing different types to be treated as instances of the same protocol. This, in turn, fosters code adaptability, scalability, and enhances code organization.

In this blog, we’ve explored the basics of protocols in Swift and demonstrated how they can be used to create powerful abstractions and foster polymorphic behavior. Whether you’re building iOS apps, macOS applications, or any other Swift-based projects, protocols will undoubtedly play a significant role in designing elegant and maintainable solutions.

By embracing protocols and incorporating them into your Swift code, you can write code that is not only robust but also adaptable to future changes. So, the next time you find yourself creating a set of requirements for objects to adhere to, consider using Swift protocols to unlock the true potential of your code! Happy coding! 🚀

Comments