Faster File Navigation

So let’s turn our attention to the editor pane. Selecting any source file from the Project Navigator will fill the content pane with that file’s contents.

Navigating with the Jump Bar

You may think that using the Project Navigator to switch between files is the fastest way to move around, but that’s not always the case. There are other ways, particularly between related files, which can be faster. Notice at the top of the editor, there’s a strip of buttons and pop-up menus. This is the Jump Bar.

images/editing/xcode-editor-jump-bar.png

For now, let’s skip the first menu item, “Recent Items” (the four boxes), and move on to the more recognizable options, like the obvious “Go Back” and “Go Forward” buttons (the left and right arrow chevrons). Like pages in a browser, these let you navigate through your file history.

To the right of Go Back and Go Forward, there’s a breadcrumb-like display of the hierarchy of what you’re currently editing: project, group, file, and symbol (i.e., a method, a property, etc.).

images/editing/xcode-editor-jump-bar-reveal-in-finder.png

Clicking on these jump bar items shows siblings within the parent structure. For example, if you click on the group folder, the pop-up shows the other groups. If you click on a source file, it shows that file’s siblings within its group.

The final item in the jump bar is individual symbols within a source file, like properties or methods. The item shown here automatically updates as you move around the file while editing, and you can click the item to show all symbols in the file and jump to one quickly. We’ll make more use of this menu later, in Organizing the Symbols Menu.

images/editing/xcode-editor-jump-bar-related-items.png

Now, turn your attention back to the leftmost icon on the jump bar, the four-boxes icon identified by the tooltip as the Related Items button. Tap it and you’ll get a pop-up menu with variety of hierarchies to navigate to files: Recent Files, Counterparts, Superclasses, Subclasses, Siblings, Callers, Callees, etc. Not all of them will apply at any given time, based on what file you’re editing and what symbol is selected (keeping in mind that just having the cursor anywhere within a given method is considered selecting it for our current purposes).

Xcode populates this menu by analyzing your code while you write it. For example, it figures out what your super- and subclasses are and creates menu items for them. It also figures out what calls or is called by the code and creates menu items for those too (“Callees” and “Callers”, respectively). These can help you notice weird behaviors and entanglements in your code that you don’t want.

Navigating to Counterparts

Back on the far left of the jump bar, one of the Related Items menu items is called “Counterparts” and this deserves special mention, because it can be a very common task. In fact, it’s so common that it has its own keyboard shortcut, , to show the next counterpart.

But that begs the question of “what the heck is a counterpart?” A counterpart is a known relationship between two files. The most obvious and common example is in C-based languages—C, C++, and Objective-C—where there are typically separate implementation and header files. These two are counterparts: if you’re editing foo.c, jumping to the next counterpart will open foo.h, assuming it exists. The same goes for other implementation file types, like m for Obj-C, cpp for C++, and mm for Objective-C++.

Only certain file types have counterparts. Storyboards, Asset Libraries, and plists don’t. For swift files, there are two programmatically generated counterparts. The first is a h file offering an Objective-C header file for your Swift members, reachable only via the Recent Items menu. The second counterpart, reachable by either Recent Items or the keyboard accelerator, is a pseudo-file of the form YourFile.swift (Interface). This is a Swift-syntax interface showing your file’s various members, but without implementations, kind of like a C-language header file. For example, for an empty view controller, the interface looks like this:

 import​ ​UIKit
 
 internal​ ​class​ ​ViewController​ : ​UIViewController​ {
 
 var​ foo: ​NSObject
 
 override​ ​internal​ ​func​ ​viewDidLoad​()
 }

Neither of these Swift interfaces shows any private or fileprivate members of your file, so this can be a good way to reveal what your file exposes to potential callers, reminding you to close off access to properties or methods you don’t want outsiders to be able to call.

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

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