Lập trình Wrox Professional Xcode 3 cho Mac OS part 23 pps

6 78 0
Lập trình Wrox Professional Xcode 3 cho Mac OS part 23 pps

Đang tải... (xem toàn văn)

Thông tin tài liệu

9 Class Browser WHAT'S IN THIS CHAPTER? Opening and navigating the class browser Switching class browser display options Customizing the class browser display Jumping to source code and documentation In the previous chapter, you saw how to search the Code Sense index for symbols and defi nitions. The class browser provides a much more structured way of exploring your project ’ s Code Sense database; it builds a structured summary of your project ’ s classes that describes their methods, instance variables, and inheritance. You can open it at any time, and it ’ s always up - to - date — or at least as current as your Code Sense index. It ’ s one of my favorite development tools, and I use it incessantly to review classes, untangle inheritance, and jump to their defi nitions. Chapter 14 shows you some additional high - level tools for analyzing your classes graphically. The best part about the class browser is that it ’ s free. You don ’ t have to do anything special to your project beyond turning on Code Sense. Once the Code Sense index for your project is built, the class browser is ready to go. NAVIGATING THE CLASS BROWSER Choose the Project ➪ Class Browser (Command+Shift+C) command to open the class browser window, shown in Figure 9 - 1. You ’ ll also fi nd a Class Browser button in the toolbar of many windows. ➤ ➤ ➤ ➤ c09.indd 179c09.indd 179 1/21/10 3:18:52 PM1/21/10 3:18:52 PM Download at getcoolebook.com 180 ❘ CHAPTER 9 CLASS BROWSER The class browser ’ s toolbar typically includes the Option Set control and the Confi gure Options button. The Option Set menu and Confi gure Options button work like the Project Find window ’ s batch fi nd options set, described in Chapter 8. The pop - up menu lets you select the named set of browser options, and the Confi gure Options button allows you to alter the options in a set or create new sets. This chapter covers option sets and the browser options shortly. Browsing Classes The class browser is broadly divided into three panes. The pane on the left is the class list. This lists all classes, interfaces, and protocols defi ned in the project. These can be organized in a hierarchy — a superclass becomes a group that contains all of the subclasses that inherit it. Expand or collapse groups using the triangle to the left of the class name. Hold down the Option key to recursively collapse or expand all subclasses. Alternatively, you can list the classes in alphabetical order. Java class names include the package so that similarly named classes in different packages can be distinguished easily. Classes defi ned in your project appear blue. Classes defi ned in frameworks are black. Selecting a class in the list displays the member elements of that class in the details pane on the right, and the source that defi nes the class in the editor pane below it. The details pane lists the member elements, grouped by type. Members defi ned in the class are black, and inherited members (if shown) are grey. Member functions and methods are listed fi rst, followed by variables. Class (static) members are listed before instance members. You can reorganize and resize the table columns by dragging in the column headers, but you cannot change the sorting order. Selecting any member in the details list displays its declaration in the editor pane below it. Double - clicking either the class name or a member jumps to that location in a new editor window. FIGURE 9 - 1 c09.indd 180c09.indd 180 1/21/10 3:18:55 PM1/21/10 3:18:55 PM Download at getcoolebook.com Getting Class Documentation If online documentation is available for a class, a small book icon appears to the right of the class name. Clicking the book icon jumps to that class ’ s documentation in the help viewer, as shown in Figure 9 - 2. FIGURE 9 - 2 Opening Multiple Class Browsers Normally, there is one class browser window for each project. It is possible to open multiple class browser windows using the New Class Browser button in the toolbar of the class browser window itself — not to be confused with the Class Browser button that appears on most other windows. This button isn ’ t in the default set of toolbar buttons; to use it you must add it to your toolbar. See the “ Toolbars ” section of Chapter 3 if you ’ re unsure how. Figure 9 - 3 shows the New Class Browser button. Multiple browser windows permit you to view different aspects of your classes simultaneously, so consider adding the New Class Browser button to your class browser ’ s toolbar (View ➪ Customize Toolbar). Navigating the Class Browser ❘ 181 c09.indd 181c09.indd 181 1/21/10 3:18:56 PM1/21/10 3:18:56 PM Download at getcoolebook.com 182 ❘ CHAPTER 9 CLASS BROWSER CLASS BROWSER OPTION SETS The class browser comes preconfi gured with four option sets. These four sets of options display just those classes defi ned in the project or all classes visible to the project. The classes can be organized in a hierarchy or listed alphabetically irrespective of relationships. Click the Confi gure Options button to edit the confi guration of a set. This displays the class browser options confi guration sheet, shown in Figure 9 - 4. FIGURE 9-4 FIGURE 9-3 Select the set of options to edit using the pop - up menu at the top of the sheet. You can edit the predefi ned sets, but avoid making any changes that run counter to their descriptive names. Changing the Hierarchy, Project Classes set to display only framework classes in a fl at list is guaranteed to cause confusion. If you want to make signifi cantly different sets of browser options, create new sets using the Add button. With the Delete button, you can delete sets you don ’ t care to use. Like the Project Find window, the class browser option sets are shared among all projects. Make changes to them judiciously. c09.indd 182c09.indd 182 1/21/10 3:18:56 PM1/21/10 3:18:56 PM Download at getcoolebook.com The left side of the sheet contains the Class List Display Settings, which consist of four options that determine which classes are included in the class browser, and how those classes are organized in the class list. The fi rst option has two choices: Hierarchical Outline groups subclasses within their super- class in a tree, and Flat List simply sorts all of the class names alphabetically. The next option has three possible settings that determine what classes are included in the list. Show Project and Framework Entries includes all classes and members defi ned in the project source and any classes defi ned in any referenced frameworks. Show Project Entries Only excludes classes defi ned in the frameworks, and Show Framework Entries Only lists only those classes defi ned in the frameworks. When you use the Hierarchical Outline display, an exception is made for superclasses. Superclasses of a class, regardless of where they are defi ned, are always included in the hierarchical display as the parents of that class. This also affects the display of inherited class members. If a project class inherits a method or variable from a framework class, and Show Project Entries Only is selected, those inher- ited members are not listed. The third option also has three possible settings: Show Classes & Protocols/Interface, Show Classes Only, and Show Protocols/Interfaces Only. This control determines whether only the class defi nitions, only protocols/interface defi nitions, or both are included in the class list. If both are included, protocols and interfaces are grouped together at the bottom of the class list. In the hierarchical view, they are listed under the title Protocols/Interfaces. In the list view, each is prefi xed with the “ Prot: ” text. The last option applies only to Objective - C and determines how Objective - C categories are organized in the class list. For those not familiar with Objective - C, a category is a way of defi ning a set of methods for a class where the defi nition and implementation of those methods is completely outside the defi nition of the class itself. In other words, it ’ s a way of “ attaching ” methods to a class without subclassing, just as if those methods had been defi ned in the class itself. (If the C++ and Java programmers in the audience are scratch- ing their heads wondering how this is possible, the magic occurs at run time — all message dispatching in Objective - C is dynamic, so new method handlers can be added to any class during program execution.) The problem is that categories don ’ t fi t in the neat hierarchy of classes and interfaces, because they are neither. The As Subclass setting treats the category as if it were a subclass in the hierarchy. The category appears as a subclass of the attached class with a “ Cat: ” prefi x. This makes it clear which class the category was written for, but is somewhat misleading because the category is not a subclass. The methods defi ned by the category are defi ned in that class and inherited by any subclasses — neither of which will be refl ected in the class browser. If you ’ re looking for the methods in a class, you have to browse not only that class but all of its categories, and any categories of its superclasses as well. The second alternative is the As Subclasses for Root Classes setting. This treats a category as though it was a subclass of the root object of the class it is attached to (almost invariably NSObject). Although this is also a fabrication, it has the advan- tage that categories are easy to fi nd because they end up grouped together at the end of NSObject ’ s sub classes. More important, the methods defi ned by a category ➤ ➤ ➤ ➤ ➤ ➤ Class Browser Option Sets ❘ 183 c09.indd 183c09.indd 183 1/21/10 3:19:02 PM1/21/10 3:19:02 PM Download at getcoolebook.com 184 ❘ CHAPTER 9 CLASS BROWSER correctly appear in the attached class and any of its subclasses. This is probably the most useful setting. The fi nal choice, Always Merged into Class, is the most accurate in that it merges the category methods into each class just as though those methods had been defi ned in that class. The categories themselves are not shown anywhere in the class list. To discover which methods of a class are defi ned by a category you must refer to the source declaration of each method. On the right are three controls that infl uence the amount of detail presented about each class: The Show Inherited Members option causes the members of a class to include all inherited members. Inherited members are displayed in grey, and the members defi ned or overridden in the class are black. The Show Methods & Data, Show Methods Only, and Show Data Only options display all members of the class, or only methods, or only instance variables, respectively. The Show Instance & Class, Show Instance Only, and Show Class Only options further refi ne the class details such that it lists all class members, or only instance members, or only class members. The term class is really an Objective - C term, where it refers to messages that can be sent to the Class object, as opposed to regular messages that would be sent to an instance of that class. In C++ and Java, class refers to static members and variables. Note that so - called static nested classes are regular classes and will appear in the browser hierar- chy within their superclass. BROWSER SHORTCUTS Xcode provides an internal editing action (Reveal in Class Browser) that jumps from the currently selected symbol in the editor to its entry in the class browser. In Xcode ’ s default key bindings, this action (sadly) isn ’ t bound to any keyboard shortcut. If you fi nd the Reveal in Class Browser shortcut useful, add a key binding for the action in the Key Bindings preferences. Key bindings are explained in Chapter 23. SUMMARY The class browser is a powerful tool for illuminating the class structure of any project. Learning to use it effectively will help you understand and navigate your code. In some ways, it is both a search tool and a documentation tool. Object - oriented programs were once said to be self - documenting. Although this is rarely true, being able to see the class structure of a project can provide insights into its organization that code comments and documentation often fail to provide. With the ability to fi nd text, symbols, and class information, you ’ re ready to start making serious changes to your code. The next two chapters help you make those changes intelligently and safely. ➤ ➤ ➤ ➤ c09.indd 184c09.indd 184 1/21/10 3:19:02 PM1/21/10 3:19:02 PM Download at getcoolebook.com . methods defi ned by a category ➤ ➤ ➤ ➤ ➤ ➤ Class Browser Option Sets ❘ 1 83 c09.indd 183c09.indd 1 83 1/21/10 3: 19:02 PM1/21/10 3: 19:02 PM Download at getcoolebook.com 184 ❘ CHAPTER 9 CLASS BROWSER. is probably the most useful setting. The fi nal choice, Always Merged into Class, is the most accurate in that it merges the category methods into each class just as though those methods had been. your code. The next two chapters help you make those changes intelligently and safely. ➤ ➤ ➤ ➤ c09.indd 184c09.indd 184 1/21/10 3: 19:02 PM1/21/10 3: 19:02 PM Download at getcoolebook.com

Ngày đăng: 04/07/2014, 06:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan