Refactoring LocationViewController

As you did before, you will add two extensions to LocationViewController. Perform the following steps:

  1. Click LocationViewController.swift in the Project Navigator. After the final curly brace, press Return a few times and add the following:
// MARK: Private Extension
private extension LocationViewController {
// code goes here
}
// MARK: UITableViewDataSource
extension LocationViewController: UITableViewDataSource {
// code goes here
}

The first one will be private. You'll put private methods for LocationViewController here. The second one will contain all of the table view data source methods.

  1. Delete UITableViewDataSource from the class declaration at the top of the file. Your class declaration should look like this when done. As before, don't worry about any errors, as they will be fixed in the next step:
class LocationViewController: UIViewController {
  1. Move all of the table view data source methods into the second extension. It should look like this when done:
// MARK: UITableViewDataSource
extension LocationViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return manager.numberOfItems()
}

func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"locationCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = manager.locationItem(at:indexPath)
return cell
}
}
  1. Just like you did in ExploreViewController, you will create an initialize() method inside the first extension, and put in it everything you need to initialize LocationViewController there. After that, you will call this initialize() function inside the viewDidLoad() method.
    Add the following inside the first extension:
func initialize() {
manager.fetch()
}

The first extension should look like this when done:

// MARK: Private Extension
private extension LocationViewController {
func initialize() {
manager.fetch()
}
}

Finally, modify viewDidLoad() as follows:

override func viewDidLoad() {
super.viewDidLoad()
initialize()
}

You are done cleaning up LocationViewController, so let's do MapViewController next by refactoring it and adding extensions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.240.21