SEARCH HISTORY As you fi nd and replace text in your project, Xcode keeps a short history of the following: Recently used search patterns Recently used replacement patterns The results
Trang 1172 ❘CHAPTER 8 SEARCHING
Symbol names are the complete, fully qualifi ed, symbol in the program If you perform a symbol search using the Whole Words option, searching for “ openFile ” will not fi nd the method openFile: , and “ willUpdateCalculator: ” will not fi nd the method willUpdate-Calculator:file: Use the Starts With or Contains option instead
The defi nition and symbol search modes only fi nd literal references to symbols in your source code For example, if the preprocessor macro CHECKPOINT expanded to code that referred to the variable testCount , a symbol search for testCount would not match a line containing the CHECKPOINT macro, even though (technically) a testCount variable reference occurs at that point in the code
SEARCH HISTORY
As you fi nd and replace text in your project, Xcode keeps a short history of the following:
Recently used search patterns Recently used replacement patterns The results of searches performed in the Project Find window Retrieving search history makes it easy to repeat a search or replacement that you ’ ve previously
done, perform a new search that ’ s a minor variation of a previous search, and review search results
even after the text that generated those results has changed
Recent Search Patterns and Replacement Text
Both the single fi le search bar and the Project Find window keep a history of the recently used
search patterns and the replacement text that was associated with it Figure 8 - 2 showed the Recent
Searches menu in the single fi le search bar Figure 8 - 11 shows the recent search pattern menu in the
Project Find window
➤
➤
➤
➤
➤
FIGURE 8-11
Trang 2In the single fi le search bar, previously used search and replacement patterns are paired; choosing a previously used search pattern also recalls the replacement text it was associated with The search pattern and replacement text fi elds in the Project Find window are independent of each other You can independently recall a previously used search pattern or a previous replacement text
New search and replace patterns are added to the top of each list, but are never duplicated in the list Reusing a search pattern does not move it back to the top of the list
Xcode remembers the mode used by a search pattern (Textual, Regular Expression, Defi nitions, or Symbol), but none of its options (word boundaries, ignore case, wrapping) If you recall a regular expression, the mode will automatically switch to Regular Expression, but if you check the Ignore Case option and then recall a previous search pattern that option will still be set
Recent Search Results
The Project Find window also remembers the last set of search results for each pattern Recalling
a previous pattern also recalls its last result set Previous result sets are also preserved in the Find Results Smart Group, described in the next section
Be careful about recalling old searches or editing fi les between the time you did the fi nd and the replace The history of search results is a set of original locations Like the single fi le Replace command, the Replace button inserts the replacement text at those locations even if the text no longer matches the search pattern
The safest practice is to get into the habit of rerunning the Find again after recalling any earlier search results or after editing any fi les
On the other hand, this particular “ feature ” can save the day If you make a mistake in the
replacement text, you can perform another replacement to correct it even if a new fi nd won ’ t fi nd
those occurrences anymore
Global Search Patterns
The current search pattern is shared globally with all projects and with the Mac OS X operating system Entering a search pattern makes it the search for every search bar and Project Find window
in Xcode In addition, the Mac OS X operating system has a global search term variable shared by all applications Applications that use this variable will set, or honor, the search pattern in Xcode
For example, search for something in BBEdit, then switch back to Xcode Whatever you searched for in BBEdit will be the current search pattern in Xcode, and vice versa
The search pattern, replacement text, and search results history is local to each project and persists only as long as the project is open
FIND RESULTS SMART GROUP
Every fi nd executed in the Project Find window adds its results to the Find Results smart group in the project window, as shown in Figure 8 - 12
Find Results Smart Group❘ 173
Trang 3174 ❘ CHAPTER 8 SEARCHING
FIGURE 8-12
Select an item in the Find Results smart group and the details pane lists the location (fi lename and
line number) and the line or symbol defi nition that was found Selecting a “ hit ” from the list displays
that location in the project window ’ s editor pane You can also double - click the item or use the
View ➪ Open In Separate Editor command to open the fi le in a new window, at the location of the line
or symbol Selecting an item in the group and pressing the Delete key disposes of the search results
Unlike the recent search results in the Project Find window, the Find Results smart group
remembers the results of every search performed — not just the last one for each search pattern
If you check the Display Results in Find Smart Group option of the Project Find window, the Find
Results smart group becomes your primary means of browsing the results of a search With this
option checked, the results list in the Project Find window disappears When a fi nd is executed, the
project window becomes active and the results of the search are automatically selected This option
really only makes sense when using the All - In - One style Leave this option unchecked to stay in the
Project Find window and review the search results there
SEARCH SHORTCUTS
Searching and replacing text during development is very common, and often repetitive Xcode
provides a number of shortcuts to keep your keystrokes and mouse clicks to a minimum The
following table lists some handy shortcuts — either keyboard shortcuts or combination commands
that perform a sequence of common actions Most apply to the text selected in the active editor
pane, but those that aren ’ t will also function in the Project Find window Many of these same
commands appear in the contextual pop - up menu in the editor pane
Trang 4TIP TO REMEMBER
Pressing the Return key while the active text selection is in either the search pattern or replacement text fi elds performs an Edit ➪ Find ➪ Next command
Make the selected text the search pattern Edit ➪ Find ➪ Use Selection for Find
(Command+E) Make the selected text the replacement text Edit ➪ Find ➪ Use Selection for Replace
(Control+Command+E) Make the selected text the search pattern and
immediately fi nd the next occurrence in the fi le
Edit ➪ Find ➪ Find in File
Make the selected text the search pattern and immediately fi nd all occurrences using the Project Find window
Edit ➪ Find ➪ Find in Project
Search for the selected text in the project using a literal string search
Right - click ➪ Find In Project ➪ As Text
Search for the selected text in the project ’ s defi nitions
Right - click ➪ Find In Project ➪ As Defi nition
Search for the selected text in the project ’ s symbols Right - click ➪ Find In Project ➪ As Symbol Find the next occurrence of the pattern in the fi le Edit ➪ Find ➪ Next (Command+G) Find the previous occurrence of the pattern in the fi le Edit ➪ Find ➪ Previous (Shift+Command+G)
Another useful combination command is the Edit ➪ Find ➪ Replace and Find Next/Previous command It performs the same function as the Replace & Find button in the editor ’ s search bar, but has no keyboard shortcut in the default Xcode bindings
SEARCHING LISTS
A third kind of search tool is located in the toolbar, as shown in Figure 8 - 13 This quick search fi eld quickly winnows any details list to only those items that match the term in the search fi eld
Searching Lists❘ 175
Trang 5176 ❘ CHAPTER 8 SEARCHING
FIGURE 8-13
The list could be a list of fi les in the project, a list of bookmarks, the project symbols, or even the
results of a project search In short, whatever you have listed in the details pane can be fi ltered using
this search fi eld
In most contexts, the search fi eld operates in one of three modes — String Matching, Wildcard
Pattern, or Regular Expression — as described in the following table Select the mode by clicking
the magnifying glass at the left end of the fi eld The current mode is displayed in grey when the fi eld
is empty, so you know what mode it is in before you enter anything
SEARCH MODE DESCRIPTION
String Matching Performs a simple textual search
Wildcard Pattern Wildcard patterns use the so - called “ globbing ” characters used by the
shell The characters * and ? will match any string or a single character, respectively A set of characters can be defi ned with the set syntax:
[chars] For example, the pattern *.h will match all of the header fi les in
a fi le list Don ’ t confuse wildcards with regular expression operators
Regular Expression The same regular expressions described earlier in this chapter
In some contexts, the search fi eld may have special modes specifi c to the type of list being displayed
The Project Symbols smart group displays the list of indexed symbols in the details list When you ’ re
displaying the symbols list, the search modes change to Search All, Search Symbols, Search Kind, and
Search Location Choosing a setting other than Search All limits the search to a particular column in
the table
To use the search fi eld, simply type something into it Whatever you enter is immediately used to
Trang 6FIGURE 8-14
entering a variable name while the details list displays contents of the Project Symbols smart group
The options in the search menu also changed When searching the symbols smart group, you can search by name, type, or fi le location Because the mode was set to Search All, the search pattern
“ check ” found the symbols checkSpelling: and checksum , but also found every symbol in the RollingChecksum.m fi le
Summary❘ 177
Clearing the contents of the search fi eld, or clicking the X button at the right end of the fi eld, cancels the search and restores the details list to its voluminous, uncensored, state
The quick search fi eld is particularly handy when digging through the results of
a search; the results of a search can be further narrowed without the need to compose a more complex search Execute a broad search in the Project Find window, switch to the Find Results smart group, and then enter a second search term in the search fi eld Only those lines that match both terms are listed
SUMMARY
You should now have at least a basic understanding of the fi le and project search tools Regular expressions are a powerful tool, but using them is often more art than science If you are new to using regular expressions, be patient After you grasp the fundamentals of regular expressions, you will be in possession of a very powerful development tool Don ’ t forget the quick search fi eld; it comes in very handy when you ’ re wading through large projects
The next chapter looks at progressively more sophisticated ways of renaming and restructuring your code using the Refactoring tool