Why And when you should use protocols in swift?

Ankit Kumar Gupta
3 min readFeb 14, 2021

Protocols are commonly known as interface in Java. So the one who is new to swift or know java as well can relate it with “interface”.

The one who is new to keyword protocol can relate it with interface in java.

Basically in Java you use interface so as to achieve multiple inheritance . The concept lies the same in swift .But also in swift, it has been suggested to use protocols , so it has been said as POP(Protocol Oriented Programming). And in my view it is a good to use protocol more often if you are in a situation where many common methods are needed in many classes. Then these methods can be wrapped inside of protocol and using this it can be used in many classes.

A protocol syntax is -

protocol A{}

If you want to declare any method you cannot directly declare it inside protocol. But you can define it. So the signature part of method can be implemented inside of protocol but implementation is restricted.

So the base structure of protocol is as below —

protocol A {

func a()

}

Also one common problem everybody has is that what to do if we want to implement only some methods of protocol and doesn’t require all of them, without using Objective-C reference.

So one very useful property which has been introduced in swift is “extension”, which is used also for code designing and cleaning.

The extension as from name itself is clear, is a property in swift which can be used to extend the functionality of any class as well of any protocol.

So we can make use of extension to make optional methods inside protocol as below —

extension A{

func b(){}

}

Just by declaring the function b() as above you have successfully made it optional. Also it should be noted that inside of protocol extension you can also provide the default implementation and also it will be override if inside of class or structure the method is provided any implmentation.

Now the question arise that how to make use of protocol .So I will just try to explain it using another example.

Let’s say I have a view called ProfileViewController , which inherits UIViewController. Inside this view the profile is shown of the user.And he want to edit his information for which he have to make changes inside another controller, EditProfileViewController.

So in this case the first view is ProfileViewController and second is EditProfileViewController.

So our main purpose is to bring data from EditProfileViewController to ProfileViewController and update the information.This can be achieved using protocol.

So I am making a protocol and a method inside is to update information.

//PROTOCOL

protocol UpdateDelegate{

func updateProfile(_ name: String)

}

//PROFILE CLASS

class ProfileViewController:UIViewController,UpdateDelegate{

@IBOutlet weak var nameLabel: UILabel!

@IBAction func editProfileButtonAction(_ sender: UIButton){

let vc = self.storyboard.instanstiateViewController(withIdentifier: “EditProfileViewController”) as! EditProfileViewController

vc.delegate = self

self.navigationController.push(vc,animated: true)

}

func updateProfile(_ name: String){

nameLabel.text = name

}

}

//EDITPROFILE CLASS

class EditProfileViewController: UIViewController{

@IBOutlet weak var nameTextField: UITextField!

var delegate: UpdateDelegate?

@IBAction func saveProfileButtonAction(_ sender: UIButton){

delegate?.updateProfile(_ name: nameTextField.text ?? “”)

self.navigationController.popViewController(animated: true)

}

}

So this common example will help you to understand the concept easily.

Thanks For Reading!

--

--

Ankit Kumar Gupta

I am a iOS developer with a experience of 5 years and had also trained in java development.I love to learn and explore my learnings.