How to Use the Xcode Source Editor for iOS App Development - dummies

How to Use the Xcode Source Editor for iOS App Development

By Jesse Feiler

The main tool you use to write code for an iOS app is the Xcode source editor, which appears as the Standard editor pane in the editor area on the right side of the Xcode Workspace window after you select a source code file in the Project navigator. It also appears as the Assistant editor in a second pane if you click the Assistant Editor button.

Apple has gone out of its way to make the source editor as useful as possible by including the following:

  • Code completion: Code completion is a feature of the editor that shows symbols — arguments, placeholders, and suggested code — as you type statements. Code completion can be really useful, especially if you forget exactly what the arguments are for a function. When code completion is active, Xcode uses the text you typed — as well as the context within which you typed it — to provide inline suggestions.

    You can accept inline suggestions by pressing Tab or Return. You can also see a pop-up list of suggestions while typing; move up and down the list with the up and down arrows. Press Return to use the selected completion. Press the Esc key, or Control+spacebar, to cancel a code completion operation. You can turn set options for code completion, by choosing Xcode→Preferences and clicking the Text Editing tab.

  • Automatic indenting, formatting, and closing braces: The source editor indents the text you type according to rules you can set in the Text Editing preferences pane. It also uses fonts and colors for the various syntax elements (variables, constants, comments, and so on) according to the settings in the Fonts & Colors pane of Xcode preferences.

    As for closing braces, anytime you type an opening brace ({) and then press Return, Xcode automatically adds a closing brace (}) — unless you’ve deactivated the Automatically Insert Closing “}” option in the Text Editing preferences.

  • Code folding in the Focus ribbon: With code folding, you can collapse code that you’re not working on and display only the code that requires your attention. You do this by clicking in the Focus ribbon column to the left of the code you want to hide. A disclosure triangle appears, and clicking it hides or shows blocks of code. The code inside awakeFromNib has been folded up.

  • Opening a file in a separate window: Double-click the file in the Project navigator to open the file in its own window.

    image0.jpg

Using Live Issues and Fix-it

The Apple LLVM compiler engine wants to be your best friend, so Live Issues continuously evaluates your code in the background and alerts you to coding mistakes. Before this feature came along, you had to build your app first, this new way saves lots of time and effort.

But not only is Live Issues happy to point out your mistakes, Fix-it will also offer (when it can) to fix the problem for you. Clicking the error displays the available Fix-its, such as correcting an assignment to a comparison, repairing a misspelled symbol, or appending a missing semicolon.

With a single keyboard shortcut, you can instantly have the error repaired, and you can continue coding. Fix-it marks syntax errors with a red underline or a caret at the position of the error and with a symbol in the gutter.

For example, the semicolon is missing after the [superviewDidLoad]statement. (Notice the error indicator — the red stop sign with exclamation point — in the Activity viewer along with the red circle in the gutter at the left of the offending line of code.) Clicking the red circle in the gutter at the left will automatically fix this problem. This is a very useful feature and will cut down your debugging time significantly.

image1.jpg

Compiler warnings

Although Xcode and the compiler working together are very good at giving you warnings and errors, sometimes they’re a little slow on the uptake when you actually get around to fixing the problem. So don’t be surprised by random warnings and errors, especially if the compiler for some reason can’t find the header file.

If you see a warning or error that you’re just sure you’ve fixed, you can click the Run button. Xcode and the compiler will reset, and the warning will go away. (Unless, of course, it was right all along and you hadn’t fixed the problem.)

The Issue navigator

The Issue navigator is one of the navigators provided by Xcode. The error also appears in the Issue navigator.

image2.jpg

To get to the Issue navigator, you select it in the Navigator selector bar.

image3.jpg

If, in spite of Live Issues and Fix-it (or any other) warnings, you decide to compile the program, the Issue navigator will automatically launch for you.

The Issue navigator displays the error and warning messages generated in a Project or Workspace and is similar to the other navigators you’ve used so far.

When you select a warning or error in the Issue navigator, an editor displays the item with the issue, and if the error occurs in a source file, the issue message is placed on the line of code with the issue.

Place the pointer over an issue message that ends with an ellipsis (which appears if the pane is too narrow to display the entire message) to get a complete description of the issue.

You can display issues by file or by type using the buttons on the Scope bar at the bottom of the navigator pane, filter the issue list with the Filter bar, and even step through issues using the Issue stepper in the Jump bar. Use the Next and Previous buttons in the Jump bar to jump to the previous and next issues.