Write Code in the Source Editor

You spend most of your development time writing, editing, and debugging code. With features like syntax correction, code completion, and static code analysis, the Xcode source editor helps you enter code quickly and accurately. Customizable features like split windows, keyboard shortcuts, and syntax-aware fonts and text colors allow you to configure the source editor to suit your work style.

To view and edit a source file, select it in the project navigator. The file’s contents appear in the editor area of the workspace window.

../art/Projects_2x.png../art/Projects_2x.png

Fix Errors as You Type

As you type into the source editor, Xcode scans your text. When you make a syntax error, Xcode marks it with a red underline or a caret. Click the error, and Xcode displays a message describing the issue.

../art/FixItError_2x.png../art/FixItError_2x.png

Often, Fix-it offers to repair your error automatically. Select a suggested correction, and press Return to accept it. In the screenshot, Fix-it suggests inserting the “@” character before the text string.

Drop Code Snippets into Your Files

Use code snippets to enter source text with minimum effort. You can drag a code snippet directly from the Code Snippet library into a source file. The Code Snippet library, which is available by clicking the code snippet button (../art/LB_Code_2x.png) in the utility area of the workspace window, provides a number of useful standard snippets, such as the switch statement snippet shown in the screenshot. You can add your own code snippets to the library.

../art/Snippet_2x.png../art/Snippet_2x.png

Create Source Files from Templates

Use file templates to add files to your project with minimum effort. The File Template library is available by clicking the file template button (../art/NB_Files_2x.png) in the utility area of the workspace window. Create a source file by dragging its template to the project navigator.

../art/FileTemplate_2x.png../art/FileTemplate_2x.png

Perform Static Code Analysis

Use the static analyzer to find bugs in your code before you even run your app. The static analyzer tries out thousands of possible code paths in a few seconds, reporting potential bugs that might have remained hidden or bugs that might be nearly impossible to replicate. This process also identifies areas in your code that don’t follow recommended API usage, such as Foundation, UIKit, and AppKit idioms.

To perform static code analysis, choose Product > Analyze. The Xcode static analyzer parses the project source code and identifies these types of problems:

The static analyzer reports problems in the issue navigator, available by clicking ../art/XG_NavArea_Issue_icon_2x.png in the project navigator selector bar. Select an analyzer message in the issue navigator to display the associated code in the source editor. Click the corresponding message in the source editor. Use the pop-up menu in the analysis results bar above the source code editor to study the flow path of the flaw. Then edit the code to fix the flaw.

Speed Up Typing with Code Completion

When you begin typing the name of a symbol, Xcode offers inline suggestions for completing the name. Click an item in the suggestion list to select it. Press Return to accept the suggestion.

../art/CodeCompletion_2x.png

When a method or function contains parameters or arguments, code completion includes a placeholder for each. To move from one placeholder to another, choose Navigate > Jump to Next Placeholder (and Navigate > Jump to Previous Placeholder).

Split the Editor to Display Related Content

Split the editor pane to see multiple views of the same file or to view multiple related files at once. For example, you can simultaneously view an implementation file and its header file counterpart. To split the source editor, open an assistant editor pane by clicking the Assistant Editor button (../art/TB_ViewSegment_Assistant_2x.png) in the workspace toolbar. The split can be vertical or horizontal.

../art/VerticalSplit_2x.png../art/VerticalSplit_2x.png
../art/HorizontalAssistant_2x.png../art/HorizontalAssistant_2x.png

To change the orientation of the split, choose View > Assistant Editor and then choose one of the menu options. In both of the screenshots above, the navigator and utility areas are closed to maximize the viewing area of the source editor.

When you open an assistant editor pane, you can set it to either of two modes: manual or tracking. In manual mode, you select the file to display by navigating to it in the jump bar. Tracking mode has several criteria to choose from, such as opening counterparts, superclasses, subclasses, and siblings. In tracking mode, Assistant selects the file that best meets your criterion and then displays it.

To change the mode, select one from the Assistant pop-up menu. (The Assistant pop-up menu is the first item to the right of the back and forward arrows in the assistant editor jump bar.)

../art/AssistantBehavior_2x.png

You can further split the assistant editor pane by clicking the Add button (../art/OpenAssistantPaneControl_2x.png) in the top-right corner of the assistant editor pane. The nearby Close button (../art/CloseAssistantPaneControl_2x.png) closes it again.

../art/SplitAssistant_2x.png../art/SplitAssistant_2x.png

Open a File Quickly

Choose File > Open Quickly to locate files that define a specified symbol or whose filenames contain a specified string. Open Quickly searches are case insensitive and are limited to the current project and to the active software development kit (SDK). From the search results list, double-click the file you want to open.

../art/OpenQuickly_2x.png../art/OpenQuickly_2x.png

To open the file in the assistant editor pane, hold down the Option key when you double-click. To open the file in a separate window, hold down Option-Shift. To see a dialog letting you specify where the file should open, hold down Option-Shift-Click.

Use Gestures and Keyboard Shortcuts

Gestures and keyboard shortcuts can simplify and enhance your use of the source editor. Besides the common Multi-Touch gestures in OS X, three are particularly applicable within the source editor:

Keyboard sequences serve as shortcuts for many common menu commands in Xcode. For example, Shift-Command-O invokes the Open Quickly command from the File menu, and Shift-Command-D invokes the Jump to Definition command from the Navigate menu. Other keyboard shortcuts assist with editing operations. For example, Control-K deletes every character from the insertion point to the end of the line.

Keyboard shortcuts are established through key bindings, which you can view and modify by choosing Xcode > Preferences and selecting Key Bindings.

Automate Extensive Changes in Your Code

When you need to make changes that apply to multiple lines of text, Xcode offers several approaches.

You can simultaneously modify all the occurrences of a symbol, such as the name of a local variable or parameter, within a scope. Place the insertion point in the symbol you want to edit. When the pop-up menu control appears, click the control to display the menu, and select Edit All in Scope. Edit the symbol. As you type new text, all instances of the symbol change simultaneously.

../art/EditInScope_2x.png

You can change instances of a text string in a single file by choosing Find > Find and Replace.

../art/FindReplaceFile_2x.png../art/FindReplaceFile_2x.png

You can change instances of a text string in your project or workspace by choosing Find > Find and Replace in Project. This command displays the find navigator. You can customize the operation—for example, to limit the scope of the search or to match the case of letters in the string. The find navigator provides a preview that allows you replace all instances of the string or to accept or reject individual replacements.

../art/SearchReplaceProject_2x.png../art/SearchReplaceProject_2x.png

You can refactor your code to improve its structure, readability, and maintainability without changing its behavior. A refactoring operation (also called a transformation) is applied to a code fragment or a symbol that you select in the source editor. You can rename symbols, extract code into methods, create superclasses, move items up to the superclasses or down to their subclasses, and encapsulate variables throughout your project files.

After selecting the code fragment or symbol you want to refactor, choose Edit > Refactor and then the appropriate refactoring command. A preview pane shows you how each change will appear when applied. Deselect a file in the leftmost pane of the preview dialog to leave it out of the refactoring operation. You can edit your source code directly in the preview dialog. Any such edits are shown in the preview and are included in the refactoring operation.

../art/RefactorRename_2x.png../art/RefactorRename_2x.png

Display the Definition of a Symbol

Place the pointer over a symbol and Command-click to display the symbol definition. The source editor navigates to the symbol definition and highlights it. If the definition is in a separate file, the source editor displays that file. (Alternatively, you can place the pointer over a symbol and choose Navigate > Jump to Definition.)

Place the pointer over a symbol and Option-Command-click to display its definition in the assistant editor pane, as illustrated for the APALoadFramesFromAtlas function in the screenshot. This approach lets you keep the symbol in view as you inspect its definition.

../art/OptionCommandClickforAssistant_2x.png../art/OptionCommandClickforAssistant_2x.png

Examine the Structure of Your Code with Code Folding

You can more easily focus your attention on a particular method or function in source code by hiding the other parts of the source code. Choose Editor > Code Folding > Fold Methods & Functions. Navigate to the method you want to unfold and double-click the ellipsis button to unfold the method. The screenshot shows the configureConnectedGameControllers method unfolded.

../art/CodeFolding_2x.png../art/CodeFolding_2x.png

Move the pointer into the focus ribbon on the left edge of the editor to display a scope—such as the for statement in the screenshot—in a focus box. Additional scopes are indicated by degrees of shading in the code.

Match Pairs of Braces, Parentheses, and Brackets Automatically

Xcode helps you balance delimiters automatically. For example:

Choose Syntax-Aware Fonts and Text Colors

Xcode parses code based on the language, and it assigns a syntactic label to each token or string—for example, each comment, keyword, class name defined in the project, and other class name. Xcode assigns a color and font to each syntactic type to make it easier for you to read the code. You can select from several font and color themes by choosing Xcode > Preferences and then selecting Fonts & Colors. For example, the Presentation theme increases the font sizes so that the text is easier to read when projected on a screen. You can also create your own custom font and color themes.

../art/PresentationTheme_2x.png

Customize Editing and Indenting Options

You can change source editing and indenting settings to suit your preferences. Choose Xcode > Preferences, and select Text Editing to modify options such as these:

Look Up Documentation for a Symbol

Find concise reference documentation for a symbol by placing the insertion point in the symbol. Click ../art/TB_ViewUtilities_2x.png in the view selector in the toolbar, and select the Quick Help button (../art/QuickHelp_2x.png) in the inspector selector bar. Quick Help for that symbol appears in the utility area.

The information includes links to complete reference documentation for the symbol, the header file where the symbol is declared, related programming guides, and related sample code. (To view in a pop-up window only a description of the symbol, its release availability, its header file, and a link to it’s related reference document, Option-click the symbol.)

../art/QuickHelpArea_2x.png../art/QuickHelpArea_2x.png

Click a link in Quick Help, and Xcode opens a separate Xcode documentation viewer window. The Xcode documentation viewer provides access to information without taking your focus away from the file you’re editing.

../art/APIRefWithMetaData_2x.png../art/APIRefWithMetaData_2x.png

The document viewer delivers in-depth programming guides, tutorials, sample code, and video presentations by Apple engineers, in addition to detailed framework API references. From a class reference, click “More related items” near the top of the viewer for links to additional documents relevant to your programming task.

../art/RelatedItems_2x.png../art/RelatedItems_2x.png

Use the Search bar to locate additional information about the API or programming concept.

../art/DocViewer_2x.png../art/DocViewer_2x.png

To include a link to the document in a message, click the Share button (../art/ShareButtonIcon_2x.png) and choose Email Link or Message. You can open the document in Safari in HTML or PDF format from this menu. For a sample code project, click Open Project at the top of the window to download the project and open it in Xcode.

../art/ShareButton_2x.png

Find Help for Using the Source Editor

Step-by-step instructions for performing common source editor tasks are available directly in Xcode. Control-click anywhere in the source editor to see a short list of the most common operations. Choose Show All Help Topics to see all help articles for the source editor. Select a task, and a help article appears in the Xcode documentation viewer window.

../art/contextual_help_2x.png../art/contextual_help_2x.png
../art/Source_editor_help_2x.png../art/Source_editor_help_2x.png

Xcode Help articles are available from shortcut menus throughout Xcode. Control-click in any of the main user interface areas to see a list of help articles available for that area.