Chapter 4. Getting Help

Nobody knows everything, which is why everyone may need help writing programs once in a while. Sometimes you may need help just figuring out how to use Xcode. Other times you may need help creating your program by looking for a specific Objective-C command or using a specific user interface object.

Of course, one of the most reliable places to look for help is to search for your problem on the Internet. Most likely, someone else has already run into your problem and has already stumbled across an answer. There are also multiple programming forums where you can post questions and get a response fairly quickly, depending on the nature of your question.

Unfortunately, you can't always rely on the Internet to help you find the answers to your problems. Sometimes people won't know the answer. Other times you can't afford to wait a day or two until someone responds. In these situations, you need to know how to find help on your own right away. When you might know what you want to do but have no idea how to do it, that's when you need to turn to Xcode's help system.

Installing Help Topics

Xcodeprovides help on a variety of topics, but to avoid overwhelming you with all available help topics, Xcode organizes help into topics such as iOS (for iPhone and iPad programming) and Mac OS X (for Mac programming). To ensure that you have the help topics for Mac OS X, you may need to load the Mac OS X help by following these steps:

  1. Choose Xcode

    Installing Help Topics
  2. Click the Documentation icon at the top of the window. (You may need to scroll these icons to the right to find the Documentation icon.)

  3. Click the Documentation Sets tab, as shown in Figure 4-1.

    The Documentation Sets tab reveals all the currently installed documentation topics.

    Figure 4-1. The Documentation Sets tab reveals all the currently installed documentation topics.

  4. (Optional) Click the Get button next to a topic you want help on, such as Mac OS X Java Library or Xcode 3.2 Developer Tools Library.

  5. Click the close button of the Xcode Preferences window to make it disappear.

Getting Help About Xcode

The easiest type of help to find is when you have questions about using Xcode such as how to use certain commands or how to modify the appearance of the Xcode window. When you need help on using Xcode, follow these steps:

  1. Choose Help

    Getting Help About Xcode
    The Developer Tools Reference Library window

    Figure 4-2. The Developer Tools Reference Library window

  2. Click a displayed topic. The window displays details about your chosen topic.

  3. (Optional) Click the Back and Forward arrows that appear in the upper-left corner of the window. This lets you quickly display information you've previously viewed.

Getting Help About Core Library

Using Objective-C and Xcode, anyone can write any type of program for the Mac. The problem is that when you need to do something routine such as saving a file to a disk or displaying a window on the screen, you would have to write your own Objective-C code to perform these common functions.

Not only does this mean you would have to spend time writing code to do routine tasks, but you'd also have to test these routines to make sure they work as well. With thousands of programmers creating their own routines, chances are good that one programmer's code to create windows won't look or work identically to another programmer's code to create and display those same type of windows.

To avoid this chaotic situation where everyone must essentially reinvent the wheel, Apple provides code stored in something called the Core Library. This collection of code performs common tasks. Just as a public library lets everyone read the same books, so does the Core Library let everyone use the same code. Any time you need to do a common task, such as save a file or play audio, chances are good that Apple has already written that code. All you need to do is figure out whether that code exists and, if it does, how to use it.

To browse through the Mac OS X Core Library, follow these steps:

  1. Choose Help

    Getting Help About Core Library
  2. Click the Home icon at the top of the left pane.

  3. Click Mac OS X Core Library in the left pane.

  4. Click the gray arrow (called a disclosure triangle) that appears to the left of the Mac OS X Core Library icon. A list of topics appears, as shown in Figure 4-3.

    The help window displays different topics about Mac OS X topic.

    Figure 4-3. The help window displays different topics about Mac OS X topic.

  5. Click the help you want to view. A list of help topic appears that you can choose.

When browsing through Xcode's Core Library help, you may see several different types of help screens:

  • Q&A

  • Tips

  • Sample programs

  • Class references

A Q&A help screen, as shown in Figure 4-4, answers a common question and provides detailed explanation for solving a specific type of problem.

A Q&A screen typically explains how to solve a specific type of problem.

Figure 4-4. A Q&A screen typically explains how to solve a specific type of problem.

A tips help screen typically provides programming guidelines for designing your programs, as shown in Figure 4-5.

A tips screen typically offers advice and tips for creating your program.

Figure 4-5. A tips screen typically offers advice and tips for creating your program.

A sample program help screen briefly describes what the sample program does and how it works so you can see whether it's something you want to study, as shown in Figure 4-6. Other screens might show the actual Objective-C code.

A sample programs screen explains what the sample program does and how it works.

Figure 4-6. A sample programs screen explains what the sample program does and how it works.

A class reference screen provides detailed information about a class that performs a specific task, which you can use in your own programs. The class reference screen lists the properties and methods stored in that class as well as any related classes, as shown in Figure 4-7.

A class reference screen provides programming details for how a particular class works and what it does.

Figure 4-7. A class reference screen provides programming details for how a particular class works and what it does.

Searching for Help

Browsing through the Xcode help window can be a great way to stumble across interesting information about using Xcode or using Apple's Core Library. However, random browsing can be tedious and time-consuming. When you need help with a specific topic, it's much faster to search through the help screens and jump straight to the information you need.

To search for help, follow these steps:

  1. Choose Help

    Searching for Help
  2. Click the Search field that appears in the upper-right corner of the Reference Library window, as shown in Figure 4-8.

    The Search field where you can type a term or phrase

    Figure 4-8. The Search field where you can type a term or phrase

  3. Click in the Search field, type a term or phrase, and press Return. The Xcode window displays topics related to your query.

  4. Click a topic to display information.

Getting Quick Help

One problem with using Xcode's help window is that you may be busy working on your program and need help on a particular Objective-C command right away. You could wade through the different Xcode help screens, or you could just use Xcode's Quick Help command instead.

Quick Help lets you select a command directly in your Objective-C code and display a tiny window of help, as shown in Figure 4-9. You can also get Quick Help by selecting an item on your user interface and choosing the Quick Help command.

Quick Help displays a window near your selected command.

Figure 4-9. Quick Help displays a window near your selected command.

To use Quick Help, follow these steps:

  1. Click an Objective-C code file (either an .h or .m file), or click a .xib file that makes up your user interface.

  2. Select an Objective-C command, or click a user interface object such as a button.

  3. Choose Help

    Quick Help displays a window near your selected command.

Viewing Documentation for Selected Text

The Quick Help command lets you view a condensed version of information about commands or objects you select. However, if you need more detailed information about an Objective-C command, you can browse through the documentation yourself, which can be time-consuming and tedious. For a faster solution, just let Xcode show you the documentation for a specific command.

To view help for a specific Objective-C command, follow these steps:

  1. Click an Objective-C code file (either an .h or .m file).

  2. Select an Objective-C command.

  3. Choose Help

    Viewing Documentation for Selected Text
You can highlight Objective-C code to view detailed help.

Figure 4-10. You can highlight Objective-C code to view detailed help.

Getting Help with Library Windows

When you're creating your program, you'll often use one of the Library windows to help you design your user interface. By clicking an item displayed in each Library window, you can view a window that explains the purpose of your selected item.

To get help with an item displayed in a Library window, follow these steps:

  1. Switch to Interface Builder.

  2. Choose Tools

    Getting Help with Library Windows
  3. Click an item, and keep the mouse pointer over that item. Xcode displays a window that describes the purpose and function of your chosen item, as shown in Figure 4-11.

Clicking an item in a Library window displays information about that item.

Figure 4-11. Clicking an item in a Library window displays information about that item.

Help While Writing Code

Even the most experienced programmer can make mistakes or forget how to spell or use a particular command. To help you write code easier and faster, Xcode provides several tools.

First, Xcode can color-code keywords and commands to make it easier to identify different parts of your code. Second, Xcode lets you modify the way the editor works so it displays code the way you like. In addition, Xcode also offers a code completion feature so you can start typing a command and have Xcode guess what command you might want to use.

Color-Coding

Xcode highlights Objective-C commands in different colors to help you identify the parts of your program. This feature is turned on by default, but you can turn it off or choose different color-coding schemes.

To modify the way Xcode displays code in different colors, follow these steps:

  1. Choose Xcode

    Color-Coding
  2. Click the Fonts & Colors icon to display the different color-coding options, as shown in Figure 4-12.

The Fonts & Colors icon lets you choose or modify the way Xcode colors Objective-C commands.

Figure 4-12. The Fonts & Colors icon lets you choose or modify the way Xcode colors Objective-C commands.

Customizing the Editor

The editor portion of Xcode is where you type Objective-C commands. To make editing simpler, you can turn on (or off) code completion, which is where Xcode tries to guess what command you're typing before you finish typing it all. In addition, you can also modify the way the editor displays your code such as changing how far it indents code or having the editor display line numbers to help you pinpoint a particular part of your program.

To modify the way the Xcode editor works, follow these steps:

  1. Choose Xcode

    Customizing the Editor
  2. Click the Text Editing icon to display the different editing options including code completion, as shown in Figure 4-13.

The Text Editing icon displays ways to modify the appearance of Objective-C code in the editor.

Figure 4-13. The Text Editing icon displays ways to modify the appearance of Objective-C code in the editor.

Using Code Completion

Code completion lets you type part of an Objective-C command and have Xcode display a likely command you may want. Then you just have to select the suggested command or keep typing if the suggested command isn't the one you want.

To use code completion, follow these steps:

  1. Type a command. As soon as Xcode recognizes a valid command, it displays a possible command in faint text, as shown in Figure 4-14.

    As you type, code completion displays a possible command.

    Figure 4-14. As you type, code completion displays a possible command.

  2. Choose one of the following options:

    1. If the suggested command is not what you want, keep typing, and Xcode may display another suggested command.

    2. If the suggested command is what you want, highlight it using the up/down arrow key, and then press Return key so Xcode types the rest of the suggested command automatically.

Through code completion, Xcode helps minimize typing errors and helps you write code faster and more accurately.

When writing Objective-C code, you often need to use curly brackets to group related code. Since multiple brackets can make identifying each pair of left and right brackets difficult, Xcode can highlight a matching bracket (or matching square bracket, or matching parenthesis) by following these steps:

  1. Move the cursor to the left of a right bracket (}).

  2. Press the right arrow key to move the cursor to the right of the right bracket (}). Xcode highlights the matching left bracket ({), as shown in Figure 4-15.

Moving the cursor across a right bracket highlights its matching left bracket.

Figure 4-15. Moving the cursor across a right bracket highlights its matching left bracket.

Summary

Although there's no substitute for a book or someone by your side who can answer your question, you can often find the help you need by digging through Xcode's help documentation.

When you know what type of task you want to accomplish but don't know the exact command to use, you may want to browse through Xcode's help. By rummaging through the help documentation, you can find Apple's library of code that you can use in your own programs.

As a general rule, only write code that's unique to what your program needs to do. When you need to do a more general task, such as saving a file or displaying graphics on the screen, chances are good that Apple already provides code that does what you need, so all you need to do is find that proper command.

When designing your user interface, you'll often need to know which methods and properties are available. For example, if you want to know how to display text in a Label object, you need to know the property to use (stringValue).

To find the properties available in an object, click that item in the Library window to display a little window, which tells you what class that user interface object is based on. (In the case of a Label object, it's based on the NSTextField class.)

Once you know the class, you can search for that specific class to view a more detailed list of all the available methods and properties for that class.

Xcode tries to provide as much help as possible, but there's no substitute for just digging into your programs, making mistakes, and fumbling around for a while. Like learning any new skill, programming the Mac may feel strange and confusing initially, but the more you do it, the easier it will get. The key is to keep practicing a little bit each day until digging through Xcode's help documentation becomes second nature and understanding Xcode's help screens gradually becomes clear and actually understandable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.234.164