In the class diagram, Right/Control+click on a node, as shown in Figure 14 - 12, and choose one of the commands: Go to Declaration Go to Defi nition Go to Documentation These same three
Trang 1You can add class nodes to the selection by holding down the Shift key while clicking unselected nodes or dragging out a selection rectangle Nodes can be individually removed from a selection
by clicking a selected node while holding down the Shift key It may be necessary to click the background area or the edge of a node; clicking the node ’ s title or a member may not deselect it
You can also type the fi rst few letters of a class name When listed alphabetically, the fi rst class that matches the characters typed is selected
Quick - Jump to Source
Double - click any class or member in the class browser, and Xcode jumps to its declaration in the source code
In the class diagram, Right/Control+click on a node, as shown in Figure 14 - 12, and choose one of the commands:
Go to Declaration
Go to Defi nition
Go to Documentation These same three commands can be found in the Design ➪ Class Model submenu If an individual property or operation
in the node is selected, Xcode jumps to that specifi c instance variable or method Otherwise, you are taken to the class ’ s defi nition, implementation, or documentation
If a class or method appears in the API documentation,
a small “ book ” icon appears next to its name in the browser
Clicking the book icon jumps to its documentation, and is the same as choosing the Design ➪ Class Model ➪ Go
to Documentation command
EDITING A CLASS MODEL
“ Editing ” a class diagram is limited to customizing its appearance As mentioned at the beginning
of this chapter, you can ’ t alter the defi nition of a class in a class model Class modeling is strictly
a visualization tool Any changes you make will be (mostly) cosmetic That said, you can alter the layout and appearance of the class diagram signifi cantly, which can profoundly infl uence its effectiveness as a programming aid
Moving Nodes
The main point of class diagrams is to visually represent the relationship between classes Creating
a pleasing and readable distribution of class nodes is, therefore, paramount to creating a useful class
➤
➤
➤
FIGURE 14-12
Download at getcoolebook.com
Trang 2model Xcode provides a variety of tools and techniques by which you can reshape a class diagram.
The inheritance lines between class nodes are permanent fi xtures and will follow the nodes as you
reposition them In fact, organizing the diagram such that all of the inheritance lines are visible and
unambiguous will be your biggest challenge
You can move nodes individually, or in groups, by selecting and dragging them to a new
position You can also use the arrow keys on the keyboard to move selected nodes
A variety of alignment commands are found in the Design ➪ Alignment ➪ Align menu Most of
these are self - explanatory, and apply to the currently selected nodes You must have at least two
nodes selected for the alignment commands to work These same commands are located in the
Alignment submenu of the node ’ s contextual menu in the diagram
Automatic Layout
Xcode provides two algorithms for automatically rearranging class nodes: hierarchical and force
-directed Select the set of nodes you want laid out — or select none to rearrange them all — and
choose the desired layout command from the Design ➪ Automatic Layout menu Hierarchical
layout, shown in Figure 14 - 13, produces graphs where sister nodes (two nodes that inherit from a
common node) are distributed horizontally For large collections of classes that all descend from the
same class (NSObject, for instance), this can create very wide diagrams
FIGURE 14-13
Force - directed layout tends to put progenitor classes in the middle of the diagram, with descendants
radiating outward Imagine that all of the nodes are negatively charged particles that are equally
repelled by all other nodes Imagine that the lines connecting the nodes are elastic bands Now,
pick up the root nodes of the model and let the remaining nodes hang like a mobile This is,
approximately, the effect of force - directed layout The diagram in Figure 14 - 13 is the same diagram
shown in Figure 14 - 12, reorganized using force - directed layout
Download at getcoolebook.com
Trang 3Hierarchical layout is the most predictable and produces extremely easy - to - see relationships, but it can produce unwieldy results for large collections of classes Force - directed layout produces compact graphs, but they are often unintuitive The release notes for Xcode also warn that the algorithm used to generate force - directed layouts is “ unbounded, ” meaning that it can take
an indeterminate amount of CPU time to compute the layout of a large and complex diagram
Xcode uses hierarchical automatic layout when a model is fi rst created, and whenever the tracking for the model is changed Automatic layout uses the current size of each node, and tries to create layouts such that nodes do not overlap Locked nodes (see the “ Locking Nodes ” section) can interfere with this goal
Resizing Nodes
You can resize nodes using the resize “ handles ” that appear on the edges of the node when it is selected Choose the Design ➪ Diagram ➪ Size ➪ Size to Fit command to resize selected nodes such that their height and width are exactly enough to show their entire names and all exposed members The height
of a node does this automatically whenever the compartments are rolled up, rolled down, expanded,
or collapsed The width, however, is never automatically adjusted If you want the width to be suffi cient to show all members, have those compartments expanded before using the Size to Fit command
You can also set the height or width of multiple nodes so that they are all identical Begin by selecting a prototype node Select additional nodes by holding down the Shift key The Size ➪ Make Same Width and Size ➪ Make Same Height commands set the width or the height of all selected nodes so that they are identical to the dimension of the prototype node
Locking Nodes
Locking a node prevents it from being moved or resized To lock or unlock a node, or nodes, use the Lock and Unlock commands in the Design ➪ Diagram or the node ’ s contextual menu Locking
is very useful for preserving the layout of a subgroup of nodes, while you add, remove, or rearrange other nodes around them
FIGURE 14-14
Download at getcoolebook.com
Trang 4Grid and Grid Alignment
As an aid to positioning and sizing nodes,
Xcode provides an optional grid The grid is
drawn in light gray behind the diagram, as
shown in Figure 14 - 15
When you ’ re dragging nodes around, their
position will normally “ snap ” to the nearest
grid line To enable or disable grid snap,
choose the Design ➪ Diagram ➪ Turn Grid
On/Off command in the main menu or simply
Right/Control+click on the background of the
diagram
Use the Design ➪ Diagram ➪ Hide/Show Grid command to hide and reveal the grid Grid snap is
independent of its visibility
Page Layout
A class diagram occupies a series of pages Like a WYSIWYG word processor or spreadsheet,
Xcode extends the graph area in page increments to accommodate the size of the diagram Drag
a node off the edge of the page, and a new page appears A solid gray line indicates the boundary
between pages If you plan to print a diagram, you can use the page guides to ensure that nodes
don ’ t straddle two or more pages If they are unimportant, the page guides can be hidden using the
Design ➪ Diagram ➪ Show/Hide Page Breaks command
The size of a page is controlled using the File ➪ Page Setup command By default, Xcode sets the
page magnifi cation to 80 percent and the orientation to landscape Smaller magnifi cation values
shrink the size of the node, allowing you to fi t more nodes on a page Consider passing out reading
glasses if you reduce the page magnifi cation below 50 percent
Changing the Tracking
You can add or remove classes to and from a class model at any time Click the diagram
background, so that no nodes are selected, and then choose the File ➪ Get Info command This
opens the Info window for the class model Switch to the Tracking tab In it, you will fi nd the list of
source fi les and groups the class model is tracking To remove fi les or groups, select them in the list
and click the - button at the bottom of the window
Changing the tracking may add, remove, or replace existing nodes This may cause customized diagrams to lose some of their customization At the very least, any change will perform an automatic hierarchical layout, undoing any hand-arranged node positions Save your document before changing your document’s tracking — or better yet, take a snapshot or check it into source control fi rst.
FIGURE 14-15
Download at getcoolebook.com
Trang 5To add new sources, click the + button This presents the sheet shown in Figure 14 - 16 Select the additional sources you want added to the model and click the Add Tracking button
FIGURE 14-16
I n its current incarnation, Xcode occasionally exhibits problems constructing class models The symptom is missing inheritance between classes, protocols,
or categories If this happens, try removing the subclass and adding it back
to the model following the classes and other nodes it inherits from If that doesn ’ t work, create a new, empty, model Add classes in batches, starting with the superclasses, in such a way that you never add a class unless all of the superclasses and interfaces or protocols from which it inherits are already in the model An easy way of accomplishing this is to keep the Class Browser window visible behind the diagram ’ s Info window That way, you can easily refer to the hierarchical arrangement of classes as you add them to the model
Download at getcoolebook.com
Trang 6Adding Annotations
Beyond altering its appearance, the only real
content that can be added to a class diagram is
an annotation node Create annotation nodes by
dragging out a rectangle using the Note tool, or
by choosing the Design ➪ Class Model ➪ Add
Comment command An annotation node has a
page - like, or sticky - note - like, appearance, as shown
in Figure 14 - 17
The content of the node is free - formed text Double - click the node to enter text edit mode All of
the typographical formatting features in the Edit ➪ Format ➪ Font and Edit ➪ Format ➪ Text
submenus can be used, which gives you an immense amount of formatting control Even the
text rulers and tab stops work, although they seem a little silly in an area only big enough for
a sentence or two
Annotation nodes can be connected to any number of other regular nodes, but not another
annotation node To connect an annotation node to a class node, use the Line tool to drag out a line
between the two nodes To remove a connection or an annotation node, select the line or node and
press the Delete key or choose Edit ➪ Delete
Customizing Colors and Fonts
You can customize the text color, font, size, and style of nodes in the Appearance tab of
the Info window Choose File ➪ Get Info with one or more nodes selected to alter the appearance
of those nodes With no node selected, the Info window allows you to set the default settings
for new nodes; the default settings apply only to new nodes created by adding additional classes to
the model
Figure 14 - 18 shows the Appearance tab The three categories — Name, Property, and Operation —
address the three compartments of each node Changing the color changes the color of the text
Changing the font, by clicking the Set button to the right of each sample, allows you to change the
font face, style, and size of the text of each compartment
FIGURE 14-17
FIGURE 14-18
Download at getcoolebook.com
Trang 7You can change the background color used in the title to distinguish between different types
of nodes — in a roundabout way Drop a color from any color source onto a node to change its background color Probably the easiest way to get a color source is to choose Edit ➪ Format ➪ Font ➪ Show Colors to display the color picker Dial a color, and then drag the color sample at the top of the picker into the node Figure 14 - 19 shows nodes with customized colors
FIGURE 14-19
Hiding Classes and Members
One of the primary uses of a class diagram is to maintain a high - level vision of class relationships — but sometimes there can be so many classes, class members, protocols, and categories that its essence
is obscured by the details You can regain some perspective by selectively hiding superfl uous classes and members You can do this on a per - class basis or automate it using rules
You might want to hide classes or certain members of classes for many reasons:
Private or protected members that shouldn ’ t be exposed Internal utility or helper classes
Base class methods, like - (NSUInteger)hash , that don ’ t add anything signifi cant to the description of the classes
Unrelated or superfl uous classes that happen to be defi ned in the same fi le as important classes
Categories that are unrelated or are used to hide private methods
➤
➤
➤
➤
➤
Download at getcoolebook.com
Trang 8Selectively hiding classes and class elements lets you pair down the diagram to exactly the detail
you want to see For example, an Objective - C class that can be archived probably implements
really doesn ’ t impart any useful information
Why you would want to hide elements also depends a lot on your goals If you ’ re using a class model
as an aide to developing your code, you ’ ll probably want to see all of the private and protected
members If your intent is to provide a class model to other developers as a means of documenting
how your classes relate and their public functionality, private and protected variables probably
shouldn ’ t be visible
You can individually control the visibility of classes, protocols, categories, and packages Or, you
can create rules to determine which classes are visible automatically You can also create rules to
determine the visibility of properties and operations; those can ’ t be set individually
Start by manually setting the visibility of classes To set the visibility of one or more classes
manually:
1 Select a node or nodes in the diagram, or select one or more classes in the class list
2 In the details pane of the browser (upper right), set the visibility of the class or classes to:
Hide per Filter Always Show Always Hide Choosing Always Show or Always Hide fi xes the visibility of the class in the diagram
Selecting the Hide per Filter option shows, or hides, the class based on the model ’ s classes fi lter When
this option is selected, a line of text below the control indicates the visibility of the class based on the
fi lter There is also a Hidden column available
in the class list of the browser Right/
Control+click the class column titles and
reveal the Hidden column in the table Click
the box in the row of each class to selectively
hide (checked), show (unchecked), or fi lter it
(indeterminate)
You can create class, property, and operation
fi lters in the General tab of the diagram ’ s Info
window Make sure no nodes are selected, and
then choose File ➪ Get Info (Command+I) to
open the Info window At the bottom of the Info
window are three fi lter settings, shown in
Figure 14 - 20
To enable a fi lter, check the box next to the
desired fi lter If disabled (or the fi lter rule is left
empty), the fi lter will not hide any nodes in the
diagram
➤
➤
➤
FIGURE 14-20
Download at getcoolebook.com
Trang 9If you ’ re conversant in Mac OS X ’ s predicate language, you can edit the predicate statement directly
in the text fi eld If you ’ re like me and need some help constructing the condition statement, click the Edit Filter button Xcode presents a predicate editor, as shown in Figure 14 - 21
FIGURE 14-21
The predicate editor allows you to graphically create arbitrarily complex conditional statements The predicate editor is described in more detail in Chapter 15, but here ’ s the short version
An expression is built from basic conditionals A condition consists of three parts: variable, operator, and value Variables are properties of each node or member in the class model, and the set of variables is fi xed For properties and operations, the variables are Name, Visibility, Type, and Static For classes, the variables are Name, Kind, Superclass, Language, and Project Member These variables are described in the following table:
VARIABLE DESCRIPTION
Name The class or member ’ s name
Kind The type of the class node in the diagram: Class, Protocol, Interface,
or Category
Superclass The name of the class that the class or category extends
Language The language in which the class was defi ned: C++, Java, or
Objective - C
Project Member This value will be Yes if the class is defi ned in the project
continues
Download at getcoolebook.com
Trang 10VARIABLE DESCRIPTION
Visibility The visibility or scope of the property or operation: Public,
Package, Protected, or Private
Type The type of the variable
Static This value will be Yes if the property or operation is a static or class
member
To confi gure a conditional, select the variable from the left side and an operation from the middle,
and then enter a value or a select a constant on the right side
You can insert a new Boolean operator into the expression by choosing the Add AND or Add OR
item in either a variable or Boolean operator menu Adding a Boolean operator makes the entire
subexpression one term in a new Boolean operator clause Each Boolean operator logically combines
the results of two or more subexpressions You can add or remove subexpressions to or from a
Boolean operator using the round plus and minus buttons on the right
To remove a Boolean operator, remove all but one of its subexpressions
You can negate a conditional or Boolean operator by selecting the Add NOT item This inserts a
negation operator, which you can remove again by choosing Remove in its menu
Figures 14 - 22, 14 - 23, and 14 - 24 present a practical example that uses three predicate
formulas to fi lter a class diagram The goal is to hide much of the minutia — simple framework
classes, common methods, private instance variables, and so on — that would otherwise clutter the
diagram
The Hide Classes rule, shown in Figure 14 - 22, hides the NSObject base class and every immediate
subclass of NSObject that is not defi ned in the current project
FIGURE 14-22
(continued)
Download at getcoolebook.com