Protocols in swift

A simple protocol implementation

There are lots of way to transfer datas between View controllers. One of the efficient and most probable way is using Protocol and delegates. Here I had implemented a protocol and used it to transfer datas between two controllers and also between multiple controllers. First you have to understand what is protocol and why we need it.

Protocol:

Similarly in Swift, you can declare a method and can use with what ever and where ever you want.

Protocol syntax:

protocol <ProtocolName>{
<Method declaration>
}

Example

Here I had declared a protocol named AddArray.

protocol AddArray{
func addArray(cities: [String])
}

Then create a view controller in story board with a tableView. In this table view array datas are going to listed.

In viewController.swift file, confirm the protocol to the ViewController class.

class ViewController: UIViewController, AddArray{
@override func viewDidLoad(){
super.viewDidLoad()
}
@override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
}

Here, Protocol must confirm to class we used means we have to implement that declared method in protocol confirmed class. Here in ViewController class the method is implemented.

func addArray(cities: [String]){
for city in cities{
print(“Hello”, city)
}
}

You can call this protocol implemented method in class at anywhere. If this is method is wanted to call from another controller that is the main support of Protocol.

Declare another class called SecondViewController to call it from there.

class SecondViewController: UIViewController{
var protocoldel: AddArray?
var array
= [“Chennai”, “Mumbai”, “Bangalore”, “Trivandrum”, “Delhi”, “Kashmir”]
@override func
viewDidLoad(){
super.viewDidLoad()
protocoldel.addArray()
}

Now for protocol delegate variable a view controller instance is passed either via segue or via pushviewcontroller.

let vc = UIStoryboard(name: "VCName", bundle: nil).instantiateViewController(withIdentifier: "VcID") as? SecondViewController 
vc.protocoldel = self
self.navigationController?.pushViewController (vc, animated: true)

Now That addArray() method get called in viewDidLoad().

And Another idea to pass or call particular data to multiple class is take that self and pass where ever you want to call that protocol for forward propogation.

Declare delegate variable in FirstViewController

static var delegate: ViewController?

Self for firstViewcontroller is passed to delegate variable in viewWillAppear().

@override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
ViewController.delegate = self
}

then we can use this self instance wherever you want the protocol method to call. In SecondViewController class

@override func viewDidLoad(){
super.viewDidLoad()
protocoldel = ViewController.delegate
protocoldel.addArray()
}

If you want any clarification regarding above concept, Feel free to ask in comments.

Happy Coding!💞

Passionate iOS developer chasing my dreams toward success