For the More Curious: Navigating Implementation Files

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.10).

Figure 15.10  Source editor jump bar

Source editor jump bar

The jump bar shows you exactly where you are within the project (including where the cursor is within a given file). Figure 15.11 breaks down the jump bar details.

Figure 15.11  Jump bar details

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 pop-up menu of that section in the project hierarchy. From there, you can easily navigate to other parts of the project.

Figure 15.12 shows the file pop-up menu for the LootLogger folder.

Figure 15.12  File pop-up menu

File pop-up menu

Perhaps most useful is the ability to navigate easily within an implementation file. If you click the last element in the breadcrumb trail, you will get a pop-up menu with the contents of the file, including all the methods implemented within that file.

While the pop-up menu 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.13 shows what you get when you filter for tableview in ItemsViewController.swift.

Figure 15.13  File pop-up menu with tableview filter

File pop-up menu with tableview filter

// MARK:

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 lifecycle
    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.14 presents a well-organized ItemsViewController.swift.

Figure 15.14  File pop-up menu with // MARK:s

File pop-up menu with // MARK:s

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.

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

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