O'Reilly logo

Swift Essentials - Second Edition by Dr. Alex Blewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Accessing repositories from view controllers

In the MasterViewController (created from the Master Detail template or a new subclass of a UITableViewController), define an instance variable, AppDelegate, which is assigned in the viewDidLoad method:

class MasterViewController:UITableViewController {
  var app:AppDelegate!
  override func viewDidLoad() {
    app = UIApplication.sharedApplication().delegate
     as? AppDelegate
    …
  }
}

The table view controller provides data in a number of sections and rows. The numberOfSections method will return the number of users with the section title being the username (indexed by the users list):

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return app.users.count } override func tableView(tableView: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required