For the More Curious: Navigating Implementation Files

Both of your view controllers have quite a few methods in their implementation files. To be effective iOS developers, 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 to help out with this (Figure 11.14).

Figure 11.14  Source editor jump bar

Source editor jump bar

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

Figure 11.15  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 popover of that section in the project hierarchy, and from there you can easily navigate to other parts of the project.

Figure 11.16 shows off the file popover in the Homepwner application.

Figure 11.16  File popover

File popover

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 still visible, you can start typing to filter the items in the list. At any point, you can then use the up and down arrow keys and then press the Enter key to jump to that method in the code. Figure 11.17 shows what you get when you search for indexpath in BNRItemsViewController.m.

Figure 11.17  File popover with indexpath search

File popover with indexpath search

#pragma mark

As your classes get longer, it can get more difficult to find the method you are looking for when it is buried in a long list of methods. A good way to organize your methods to help with the mess is by using the #pragma mark preprocessor directive.

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​V​i​e​w​ ​l​i​f​e​ ​c​y​c​l​e​
-​ ​(​v​o​i​d​)​v​i​e​w​D​i​d​L​o​a​d​ ​{​.​.​.​}​
-​ ​(​v​o​i​d​)​v​i​e​w​W​i​l​l​A​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​ ​{​.​.​.​}​

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​A​c​t​i​o​n​s​
-​ ​(​v​o​i​d​)​a​d​d​N​e​w​I​t​e​m​:​(​i​d​)​s​e​n​d​e​r​ ​{​.​.​.​}​

Adding #pragma marks to your code does not change anything with the code, but instead helps Xcode understand how you would like to visually organize your methods. You can see the results of adding them by opening the current file item in the jump bar. Figure 11.18 shows the results of a well-organized BNRItemsViewController.m.

Figure 11.18  File popover with #pragma marks

File popover with #pragma marks

Two useful #pragma marks are the divider and the label.

/​/​ ​T​h​i​s​ ​i​s​ ​a​ ​d​i​v​i​d​e​r​
#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​

/​/​ ​T​h​i​s​ ​i​s​ ​a​ ​l​a​b​e​l​
#​p​r​a​g​m​a​ ​m​a​r​k​ ​M​y​ ​A​w​e​s​o​m​e​ ​M​e​t​h​o​d​s​

/​/​ ​T​h​e​y​ ​c​a​n​ ​b​e​ ​c​o​m​b​i​n​e​d​ ​a​s​ ​w​e​l​l​
#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​M​y​ ​A​w​e​s​o​m​e​ ​M​e​t​h​o​d​s​

By using the #pragma mark directive, you force yourself to organize your code. If done well, this will make your code more readable and easier for you to work with when you inevitably need to revisit the code. After doing it repeatedly, you will build habits which will further help you navigate your code base.

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

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