Data Source

Introduction

Welcome to the last lesson of Protocol Oriented Swift. In the previous lesson, you’ve learned how to pass data from the CEO to the secretary. In this lesson, you will learn how to pass data from the secretary to the CEO. Lastly, you will learn how the datasource pattern is used within the iOS ecosystem using UITableView. It’s not much different from the previous lesson. Let’s get started.

Problem

Can the CEO hear from the Secretary?

Purpose

Purpose of Data Source: Communicate (Backward) from SecondVC(delegate) to FirstVC(delegator)

It is also a part of the delegate pattern. Do not get caught up with the name, “data source”. Just follow me along.

Design Protocol

  1. protocol PassDataDelegate {
  2. func passData(data: String) -> String
  3. }

There is only one difference. The passData method return String. You will find out how everything works together.

Design Delegator (Sender/CEO)

  1. class FirstVC {
  2. var delegate: PassDataDelegate?
  3. }
  4. FirstVC().delegate?.passData(data: "a bunch of contracts") // nil

Design Delegate (Receiver/Secretary)

  1. class SecondVC: PassDataDelegate {
  2. func passData(data: String) -> String {
  3. print("The CEO gave me \(data)")
  4. return "I'm too tired..."
  5. }
  6. }

The SecondVC object contains a method passData. Unlike the previous tutorial, the method now returns String.

Create Objects

  1. let firstVC = FirstVC()
  2. let secondVC = SecondVC()

Everything is the same.

Assign Delegate

  1. firstVC.delegate = secondVC

Execute the Method

Here is the difference.

  1. let message = firstVC.delegate?.passData(data: "a bunch of contracts!")
  2. print(message!) // "I'm too tired"

The firstVC object now hears “I’m too tired” which is an implementation by the secondVC object.

Practical Examples in iOS

Let us find out how the return part is used within the iOS ecosystem.

  1. import UIKit
  2. class BobViewController: UIViewController, UITableViewDataSource {
  3. var tableView = UITableView()
  4. override func viewDidLoad() {
  5. super.viewDidLoad()
  6. tableView.dataSource = self
  7. }
  8. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  9. return 2000
  10. }
  11. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  12. return UITableViewCell(style: .default, reuseIdentifier: "cell")
  13. }
  14. }

The BobViewController object must contains two methods which are numberOfRowsInSection and cellForRowAt. The object must return Int and UITableView to the tableView object. The tableView object then uses the data received from the BobViewController object to generate its UI and all sorts of things we developers have no idea since the UIKIt framework ain’t open sourced.

Source code

4004_delegate.playground

Resources

The Complete Understanding of Delegate and DataSource

Introduction to Delegate in Swift

Conclusion

When you first started this lesson, you must have thought data source must be something extraordinary and difficult. Not at all. It is still a part of the delegate pattern, yet it is nothing more than a method that returns. For the naming purpose, if you think the secretary must talk to the CEO, then call the secretary as the ~datasource. If not, just name it as ~delegate. If you’ve completed understood the delegate pattern with me, great job. You are ready for the next advanced iOS course.