Write your code

After you create the user interface, you need to implement the action methods that Interface Builder added to your source files. To view and edit a source file, select the file in the Project navigator (), and the file contents appear in the source editor on the right. To open the source file in a separate window, Control-click the file and choose Open in New Window from the pop-up menu.

As you type in the source editor, Xcode parses your code. If a syntax error is detected, a message appears next to the corresponding line of code. To read the full message that describes the issue and suggests fixes, click the error or warning icon. Then click on the Fix button next to a suggestion to update your code.

You can avoid most syntax errors by using 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 or use the Up Arrow or Down Arrow keys to select it. Then press Return to accept the suggestion.

When a method or function contains parameters or arguments, code completion includes a placeholder for each. To move to the next placeholder, press Tab; to move to the previous placeholder, press Shift-Tab.

To find and replace text in the source editor, choose Find > Find and Replace (or press Option-Command-F). The search controls appear above the source file. As you type the search string in the Replace field, Xcode searches the file and highlights the occurrences of the string. Enter a replacement string in the With field. To narrow the search, use the controls on the right side of the Replace field. To add a URL, email address, or IP address, click the Add button (+), then choose a pattern from the pop-up menu. To learn what a control does, hover the pointer over it. To replace the occurrence and find the next one, click Replace. To replace all occurrences, click All.

To find strings in your entire project, choose Find > Find in Project or select the Find navigator () in the navigator area. Enter text in the search field followed by Return, and Xcode displays occurrences of the string in the entire project. Select an occurrence to view it in the editor area. To scope the search, above the text field, choose a string type and options. To find all occurrences that start with a string, choose Find > Text > Starting with. To find symbols that start with a string, choose Find > Definitions > Starting with.

You can perform some actions on structures in your code such as methods and symbols. Hold the Command key and move the pointer around your code to highlight the different structures. Command-click the highlighted structure, then choose an action from the Action menu.

For example, to collapse a method or other structure, choose Fold. Alternatively, use the code folding ribbon to quickly fold and unfold structures. To show the code folding ribbon, open Text Editing preferences, click Editing, then select the “Code folding ribbon" checkbox. A grey ribbon appears in the gutter between the line numbers and the structures and comments you can fold. To fold a section of your code, hover the pointer over the adjacent ribbon, then click anywhere in the control that appears. For more code folding actions, choose Editor > Code Folding.

To edit all occurrences of a symbol in the current scope, Command-click the symbol, then choose Edit All in Scope from the Action menu. Xcode highlights all occurrences of the symbol in the current scope. As you edit the symbol, all occurrences of the symbol change. Press Return to accept the changes or Escape to cancel.

To rename a symbol in the entire project, Command-click the symbol, then choose Rename from the Action menu. The edit area shows all source, Interface Builder, and Core Data files where the symbol occurs. As you edit the symbol, all occurrences are symbol change. Press Return to accept the changes or Escape to cancel. For example, choose Rename when you want to change the name of an outlet or action in your code so that you don’t break the connections you made from user interface files.

You can use multiple cursor editing to save time. To insert multiple cursors in a file, press and hold Control-Shift, click each location where you want to insert a cursor, then release Control-Shift. For example, use multiple cursors to change the parameters of common method calls at once. To select a range of strings across multiple lines, Option-Shift-drag across the lines. For example, select all the types in variable declarations that appear on separate lines. To cancel multiple cursor editing, press Escape or click anywhere in the file.

To view a symbol declaration, Command-click the symbol, then choose Jump to Definition from the Action menu. If there’s more than one definition for a symbol, select a file and line location from the dialog that appears. The file containing the definition is opened to the declaration of the symbol. To open the file in the assistant editor, hold the Option key when you choose Jump to Definition. To remove the assistant editor, click the close button in the upper-right corner.

To jump to a line of code that calls a function or method, Command-click the symbol, then choose Caller or Callers from the Action menu. If there’s more than one caller, select a file and line location from the dialog that appears. Xcode opens the source file containing the line of code that calls the function or method.

To read a description of a symbol, Command-Click the symbol, then choose Show Quick Help from the Action menu (alternatively, Option-click a symbol). The Quick Help popover appears containing a brief description of the symbol. To view the full documentation, click the Open in Developer Documentation link at the bottom of the popover. To dismiss the popover, press Escape or click anywhere in the file.

To open a file in your project or in the system frameworks that contains specific text, choose File > Open Quickly (Command-Shift-O) from anywhere in your project. In the search field popover, enter the desired text. The list of matching files updates as you type. Double-click an item in the list to open the file in the main editor. Click the Swift button () to toggle between showing Swift files and Objective-C files.

Next Step: Debug your app.

See alsoAdd files and folders to a project