iOS Reference Library Apple Developer
Search

Searching Files and Projects

Being able to find information—knowing how to locate items in a project, as well as knowing how to find information about your project—is critical to working effectively in Xcode.

Xcode gives you many ways to locate project information and items. Xcode Workspace Guide describes the common paradigms of the Xcode user interface that let you find and manage project contents, including the Groups & Files list, which lets you organize and access the items in your project in an outline view, and the detail view, which lets you quickly filter your project contents. In addition, the Activity Viewer lets you see additional information on Xcode operations, while Info windows let you examine and modify items in your project.

“Opening Files by Filename or Symbol Name” in Xcode Workspace Guide describes shortcuts you can use to open a file whose name or path appears in an editor. “Searching Documentation” in Xcode Workspace Guide describes shortcuts you can use to jump to the documentation for a symbol whose name appears in the editor or search the installed documentation for a word or phrase.

This chapter describes how to perform text-based searches on files and projects, using string match or regular expressions. It also shows how to use the Project Symbols smart group to find information on the symbols in a project.

Searching in a File

This section shows how to search text within a file. Xcode uses the same mechanism to let you search and replace text in the text editor and the property-list editor.

Searching Text

To search for text in a file that you have open in the editor, choose Edit > Find > Find.

The search bar appears below the editor toolbar, as shown in Figure 4-1.

Figure 4-1  The search bar on a text editor window

Single File Find window

The Find/Replace pop-up menu lets you choose between Find and Find & Replace. For more on Find & Replace, see “Replacing Text.”

You can search using a text string or a regular expression; choose the appropriate search type from the search-options menu in the search field:

Type the text string or regular expression pattern to use for the search in the search field. To minimize your typing, Xcode keeps track of search strings; to reuse a previous search string, choose it from the Recent Searches group in the search-options menu. You can clear the recent-searches list by choosing Clear Recent Searches from the search-options menu.

The other options in the search-options menu give you additional control over how the search is performed; these options are:

Use the Next and Previous buttons (to the left of the search field) to continue searching for the same text in a file. You can also use menu commands. Choose Edit > Find > Find Next to move to the next match and Edit > Find > Find Previous to move to the previous match.

Table 4-1  Search commands

Command

Action

Move to next match

Press Return.

Dismiss the search bar

Press Escape.

Replacing Text

You can replace some or all occurrences of text matching the string or regular expression specified in the search field.

Tip: Xcode refactoring provides a more efficient way of renaming a symbol throughout multiple files. See ‚ÄúRefactoring‚Äù for details.

To search for text in a file and replace it with other text, do one of the following:

The search & replace bar appears, as shown in Figure 4-2.

Figure 4-2  The search & replace bar on a text editor window

Use the replace buttons to perform the text substitution. The scope of the replacement varies, depending on the button you choose. Here are the buttons available to you:

Each of these buttons also has a menu item equivalent in the Edit > Find menu: Replace, Replace All, Replace and Find Next, and Replace and Find Previous.

Table 4-2  Search & replace commands

Command

Action

Replace the current match and move to next match

Press Return.

Dismiss the search & replace bar

Press Escape.

Searching in a Project

Xcode provides a number of ways to search for information in a project. You can search for text, regular expressions, or symbol definitions in a single file or across multiple files in your project. You can also easily substitute replacement text for one or more instances of matching text or symbols, either within a file or throughout the entire project.

This section describes how to use projectwide search features in Xcode to search through multiple files in your project and its included frameworks for text, regular expressions, and symbol definitions. This section also describes how to view search results. The single-file find and shortcuts for performing searches from an Xcode editor window are discussed further in “Navigating Source Files” in Xcode Workspace Guide.

For more information on Code Sense, the technology that provides symbol definition searches, see “Symbol Indexing.”

The Project Find Window

The Project Find window allows you to search for information in some or all of the files included in your project. Using the Project Find window, you can search your project for text, symbol definitions, or regular expressions. To open the Project Find window, choose Edit > Find > Find In Project. A window similar to the one in Figure 4-3appears.

Note: In the All-In-One project window layout, choosing Edit > Find > Find In Project opens the Project Find pane in the project page. The Project Find pane contains the same information as the Project Find window shown in Figure 4-3.

Figure 4-3  The Project Find window

The Project Find windowThe Project Find window

Choosing What to Search For

Using the fields and menus at the top of the Project Find window, you can control what Xcode searches for.

As a shortcut, you can also perform a quick search of selected text or regular expressions in an editor window, as described in “Shortcuts for Finding Text and Symbol Definitions” in Xcode Workspace Guide.

Specifying Which Files to Search

To control the scope of a search, use the pop-up menu to the right of the Find field in the Project Find window. This menu contains sets of search options that specify which projects and frameworks to search in. Xcode provides the following default sets of search options:

You can further tailor these default sets of search options or define your own sets with the Batch Find Options window, described in “Creating Sets of Search Options.”

Creating Sets of Search Options

The Batch Find Options window lets you further tailor the scope of the search. You can further narrow which files and projects are searched, filter the list of files to be searched according to a regular expression, and add directories to the list of locations to search.

You can modify default sets of search options or define your own sets. Defining your own set is particularly useful if you find yourself searching the same set of files over and over again. Instead of configuring the set of files to search each time, simply configure it once and save it as a search option set. To reuse the search option set, simply choose it from the pop-up menu next to the Find button in the Project Find window.

To open the Batch Find Options window, click the Options button in the Project Find window. You should see a window similar to the one in Figure 4-4.

Figure 4-4  The Batch Find Options window

The Batch Find Options window

The Find Sets menu at the top of the Batch Find Options windows lists the available search option sets. To create a new set, click Add. Specify a name for the new set in the dialog that appears. Xcode creates a new set of search options with default values. To delete a set of search options, choose that set from the Find Sets menu and click Delete.

To edit a search option set, choose that set from the Find Sets menu and set the search options you wish to include. The Batch Find Options window provides the following options to control which files Xcode searches:

You can further restrict the files that are searched by a search option set using the radio buttons on the right side of the Batch Find Options window. You have the following options:

Viewing Search Results

When you perform a projectwide find, the results of the search are listed below the search criteria; results are organized according to the file in which they appear. You can view a particular search result in the file it was found in by selecting it in the Project Find window; Xcode opens the file to the matching text and displays it in the attached editor. Double-click a search result to open it in a separate editor window.

Each search, and its results, is also collected in the Find Results smart group that appears in the Groups & Files list of the project window, as shown in Figure 4-3. If you select the Display Results in Find Smart Group option, Xcode automatically brings the project window to the front and discloses the contents of the Find Results smart group when you perform a search, instead of showing the results in the Project Find window.

You can see all the searches you have performed by clicking the disclosure triangle next to the Find Results smart group in the Groups & Files list. To view the results of a given search, select that search in the Groups & Files list and, if necessary, open a detail view. The detail view shows all of the results for the selected search, as shown in Figure 4-5. You can view the combined results of several searches by selecting those searches in the Groups & Files list. Double-clicking an item in the Find Results group in the Groups & Files list opens a Project Find window with the corresponding search specification as well as a detailed find results list. This allows you to rerun previous searches.

Figure 4-5  Search results in the project window

Search results in the project windowSearch results in the project window

To delete a search result from the Groups & Files list, select the result and press Delete.

The detail view shows the context for each search result and the file in which the match occurs. The context of a search result is the surrounding text in which it appears for text searches, and the type and name of the matching symbol for a symbol definition search. To show or hide either of these two columns, use the View > Detail View Columns menu items or Control-click in any column header and choose the desired column from the contextual menu.

To view the source for a particular result, select the result in the detail view. If the project window or detail view has an attached editor, selecting a search result displays the source in the editor. You can double-click a search result to open the source for the result in a separate window.

You can sort the results of a search according to the file in which they occur. Click the Location column heading to sort the detail view by location. You can also filter the search results using the search field in the project window toolbar. Using the location of the search result as an example again, you can type all or part of a filename to see only those results that occur in the file of that name.

Replacing Text in Multiple Files

You can use the Project Find window to replace some or all occurrences of the search string specified in the Find field. To replace text in multiple files:

  1. Type the substitution text in the Replace field

  2. Select one or more entries to replace. To choose which occurrences of the given search string to replace, do either of the following:

    • Select one or more entries to replace in the find results pane of the Project Find window.

    • Choose a search from the Find Results smart group in the project window. In the detail view, select one or more occurrences of the search string to replace.

  3. Click the Replace button in the Project Find window.

If the contents of the Project Find window’s find results pane are disclosed, Xcode uses the selection in the Project Find window to determine which occurrences to replace. If the find results group is closed, Xcode uses the selection in the detail view of the project window.

Shortcuts for Finding Text and Symbol Definitions

Xcode provides a number of shortcuts for searching that use text or other content that appears in the text editor. You can use these shortcuts to perform single-file and projectwide searches without going through the Single File Find or Project Find windows. When you use these shortcuts, Xcode performs the search using the same options specified the last time you used the Single File Find or Project Find windows. These windows are described in further detail in “Searching in a File” and "Searching in a Project" in Xcode Project Management Guide.

To search the current project for text that appears in the text editor, select the text to search for, and choose Edit > Find > Find Selected Text.

To perform a projectwide search using the current selection in the text editor, use the shortcuts listed in Table 4-3.

Table 4-3  Shortcuts for performing a projectwide search using the current selection in the editor

Search project for

Choose

Selected text

Edit > Find > Find Selected Text in Project

Selected symbol definition

Editor shortcut menu > Find > Find Selected Definition in Project

You can also jump directly to the definition for a symbol identifier by doing either of the following:

Each of the searches described in Table 4-3 uses the last set of search options used when searching your project. If you want to perform a projectwide search using the current selection in the text editor, but do not want to use the last set of search options, you can open a Project Find window with the current selection by doing either of the following:




Last updated: 2010-07-02

Did this document help you? Yes It's good, but... Not helpful...