Both of your view controllers have quite a few methods in their implementation files. To be an effective iOS developer, you must be able to go to the code you are looking for quickly and easily. The source editor jump bar in Xcode is one tool at your disposal (Figure 15.15).
The jump bar shows you where exactly you are within the project (and also where the cursor is within a given file). Figure 15.16 breaks down the jump bar details.
The breadcrumb trail navigation of the jump bar mirrors the project navigation hierarchy. If you click on any of the sections, you will be presented with a popover of that section in the project hierarchy. From there, you can easily navigate to other parts of the project.
Figure 15.17 shows the file popover for the Homepwner folder.
Perhaps most useful is the ability to navigate easily within an implementation file. If you click on the last element in the breadcrumb trail, you will get a popover with the contents of the file, including all of the methods implemented within that file.
While the popover is visible, you can type to filter the items in the list. At any point, you can use the up and down arrow keys and then press the Return key to jump to that method in the code. Figure 15.18 shows what you get when you search for “tableview” in ItemsViewController.swift.
As your classes get longer, it can get more difficult to find a method buried in a long list of methods. A good way to organize your methods is to use // MARK: comments.
Two useful // MARK: comments are the divider and the label:
// This is a divider // MARK: - // This is a label // MARK: My Awesome Methods
The divider and label can be combined:
// MARK: - View life cycle override func viewDidLoad() { ... } override func viewWillAppear(_ animated: Bool) { ... } // MARK: - Actions func addNewItem(_ sender: UIBarButtonItem) {...}
Adding // MARK: comments to your code does not change the code itself; it just tells Xcode how to visually organize your methods. You can see the results by opening the current file item in the jump bar. Figure 15.19 presents a well-organized ItemsViewController.swift.
If you make a habit of using // MARK: comments, you will force yourself to organize your code. If done thoughtfully, this will make your code more readable and easier to work with.