Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
This chapter is from the book

Using the Text Editor

The text editor in Xcode is similar to many text editors that you have probably used already. Two areas deserve your attention even if you are used to using text editors:

  • Editing preferences—Xcode provides extensive preferences for displaying and auto-completing code. Even if you have used other text editors, take a quick look at these preferences so that you can find out what’s new in Xcode and, if you are used to another text editor, how to customize colors and behaviors to what you are used to.
  • Fix-it and Live Issues—The LLVM compiler in Xcode 4 is not just for formal compiles. Its engine runs in the background checking syntax as you type so that errant keystrokes are caught in many cases as soon as you make them. Not only is the LLVM engine looking for misspellings, but it is aware of common syntax errors that can take a long time to track down, even though they are absurdly simply (once you know what the error is). One such error is demonstrated in this line of code that almost every developer has typed more than once:

    if ( x = 3 ) { ...

    That is a replacement statement, not a logical comparison. Fix-It would most likely suggest the following:

    If ( x == 3 ) { ...

Setting Editing Preferences

As in most Mac apps, preferences are set from the application menu (that is, the Xcode menu in this case). Tabs at the top let you set different collections of preferences, and, as in the case of text editing, further tabs let you set more details such as the editing and indentation preferences.

Figure 1.36 shows the editing preferences. Most are familiar to users of other code text editors, but two may be new to you. The code folding ribbon appears to the right of the gutter and the left of the main text editing area. It lets you collapse blocks of code so you can focus on other areas. If the code folding ribbon is shown, you have a further option—to focus on code as you hover the pointer over it.

Figure 1.36

Figure 1.36 Set editing preferences.

Figure 1.37 shows this behavior in action. Note the folded code in shouldAutorotateToInterfaceOrientation.

Figure 1.37

Figure 1.37 Highlight blocks of code by hovering over them.

Many people have this option on at all times. As you move the mouse over code, you will quickly spot unmatched brackets or quotation marks because the highlighted block of code will be illogical.

Syntax-aware indenting can be set, as shown in Figure 1.38. Just as with the highlighting of code in the code ribbon, this can provide an early warning of unbalanced punctuation.

Figure 1.38

Figure 1.38 Syntax-aware indentation makes your code neater and catches some keystroke errors as well.

The final preference you should look at is Fonts & Colors, as shown in Figure 1.39.

Figure 1.39

Figure 1.39 Set Fonts & Colors.

A variety of predefined styles is available, and you can switch back and forth among them as you wish. The color wells at the bottom of the window bring up a color picker for you to use to replace any of the colors in the theme for the syntax element that you have highlighted in the main body of the window. The font for the highlighted syntax element is identified in the font field; click the T at the right of the field to bring up the font panel and change the size, style, or font.

Among the provided themes is one called Presentation. For some people, this is one of the most frequently used themes. Whereas the other themes ship with fonts that are 11 points, the Presentation theme ships with an 18-point font. Not only is Xcode used to build Mac OS X and iOS as well as Apple apps, it is also often used to prepare slides for conferences such as the Worldwide Developer Conference—and that is where the presentation theme comes in handy. Even if you are not presenting at WWDC, the Presentation theme can be useful for code reviews and documentation in your own organization.

Using Fix-It and Code Completion

Xcode is constantly indexing your project and its files in the background. As it does so, it can provide code completion (type-ahead) tips for you. Figure 1.40 shows this feature in action. As you type each character in a symbol name, a list of the possible completions appears. You can select one of them or continue typing to narrow down your search. In many cases (such as your own variables), there is no list; there is just a grayed-out completion displayed. Pressing Return accepts the completion.

Figure 1.40

Figure 1.40 Use code completion.

You will note that in a case in which there are alternatives, Xcode indicates what type of object each one is. Figure 1.40 shows several classes and typedefs.

Finally, the LLVM engine tries to catch syntax errors as soon as you type them. It will flag them with warnings or errors in the gutter; clicking the symbol will bring up the error itself and, if possible, a Fix-It, as shown in Figure 1.41.

Figure 1.41

Figure 1.41 Use Fix-It.

You can press Return to accept the Fix-It.

  • + Share This
  • 🔖 Save To Your Account