If the spelling checker cannot guess the correct spelling of the word, you can help it by editing the suspect word and telling it to guess again using the Guess button.. Checking One Wor
Trang 1Undoing a Cut or Copy command does not restore the previous contents of the system ’ s clipboard It only restores the state of the editor pane prior to the Cut command
The Redo command performs the complement of the Undo command It takes the most recent
action moved to the redo list, performs it again, and adds it to the undo history Together, the Undo
and Redo commands are like a time machine, allowing you to step forward and backward through
the history of the fi le ’ s changes until you fi nd the state of the fi le you want
Any new change made to a fi le erases the redo list If you go back in time and make a different
choice, you can no longer visit the future that contained the original choice The Revert to Saved
command empties both the undo and redo information for a fi le
The Save command does not alter the undo or redo information for a fi le This means it is possible
to undo changes made to a fi le after it has been saved Whenever you try to undo the last action that
occurred before a fi le was saved, Xcode presents a dialog box like the one shown in Figure 6 - 25
FIGURE 6-25
Some developers fi nd this dialog box annoying Chapter 19 explains how to set the
XCShowUndoPastSaveWarning advanced setting to disable that warning
SHELL SCRIPTS
If you ’ ve been around the Macintosh platform long enough to remember MPW (the Macintosh
Programmer ’ s Workshop), you may be missing one of its more intriguing features Every text
window in MPW was a “ live ” worksheet, capable of executing any MPW command In fact,
MPW made no real distinction between a text document and a shell window This made for some
interesting possibilities
Trang 2Xcode resurrects this ability, in a fashion, with the hidden Execute Script command This command
is normally bound to the Control + R key combination It doesn ’ t appear in any of the menus Select any text and press Control + R The selected text is executed in your default shell (bash, by default)
The output of those commands is inserted into your text fi le immediately after the selected script, as shown on the right in Figure 6 - 26
FIGURE 6-26
The current directory is always set to the active project folder prior to execution, so you can refer
to project fi les using project folder – relative paths A new instance of the shell is used for each invocation, so aliases and shell variables are not persistent Note that the shell ’ s stdin is set to /dev/
null , making it impossible to use commands that prompt for user input, such as sudo
SPELL CHECKING
Programmers are notoriously bad spellers Thanks to the Cocoa underpinnings of Xcode, the editor inherits the standard Mac OS X spelling checker The spelling checker is great for correcting comments and documentation, thus avoiding the scorn and ridicule of the documentation
department that is inevitably populated with English majors and spelling bee champions The spelling checker is essentially useless for code Avoid adding program symbols or language keywords
to your user dictionary Your login account has only one user dictionary, and fi lling it with programming symbols defeats its usefulness in other applications
Interactive Checking
Start the interactive spelling checker with the Edit ➪ Spelling ➪ Show Spelling & Grammar (Shift + Command + :) command This opens the Spelling and Grammar palette (see Figure 6 - 27)
Trang 3Checking starts at the text cursor or the beginning of the current selection Checking is not
limited to the text selection; it just starts there The next word that the spelling checker suspects is
misspelled is highlighted in the text and displayed in the entry fi eld of the palette Above the suspect
word is a list of suggested corrections
The Ignore button adds the suspect word to the temporary ignore list The spelling checker ignores
this word, and assumes that it is spelled correctly, until the next editor session Use this to teach the
spelling checker temporarily about correctly spelled words, without permanently adding them to
the dictionary
The Find Next button skips this word and goes looking for the next suspicious word The spelling
checker still considers the word to be misspelled
You can immediately replace a word with a suggestion from the Guess list by selecting the
suggestion and clicking the Change button, or by double - clicking a suggestion in the list You can
replace the word with any arbitrary text by editing the contents of the text fi eld before clicking the
Change button
If the spelling checker cannot guess the correct spelling of the word, you can help it by editing the
suspect word and telling it to guess again using the Guess button For example, the spelling checker
cannot guess that you mean “ exhausted ” if you ’ ve actually typed “ eghosted ” Replacing the “ g ”
with an “ x ” and clicking the Guess button, as shown in Figure 6 - 28, gets the word close enough for
the spelling checker to fi nd the correct word
FIGURE 6-27
Trang 4The pop - up at the bottom of the palette lets you choose a different language The Learn and Ignore buttons take the word currently in the entry fi eld and either add or remove it from your user dictionary To remove a word you previously added, you have to type or paste it into the fi eld before clicking the Ignore button Your user dictionary is shared by all applications that use the Spelling Checker interface, so add to it wisely and sparingly
Checking One Word
You can invoke the spelling checker without bringing up the spelling checker palette with the Edit
➪ Spelling ➪ Check Spelling (Command + ;) command This command starts at the current location
in the fi le and fi nds the next suspect word, which it highlights That ’ s it Edit the word in the editor pane or use the command again to fi nd the next suspect word
This is probably the most useful spell checking command for programmers After writing a long comment, position the cursor at the beginning of the comment and press Command + ; The spelling checker will skip to the fi rst suspect word in the comment Once you ’ ve corrected all of the English words in the comment, simply go back to writing code
Checking While Typing
The Spelling menu has an option to Check Spelling as You Type If selected, words that you just typed or edited are spell checked and highlighted automatically Note that this does not spell check the entire document, or text that you paste or drag It only checks the word that your cursor was
just in or adjacent to The word that the cursor is currently in or adjacent to is never highlighted
(because it is assumed that you are in the process of changing it)
You may fi nd this feature useless and annoying when you ’ re editing source code, because practically every “ word ” in your program is going to be tagged as misspelled However, if you are writing a large amount of documentation in Xcode, you might fi nd it helpful to turn it on temporarily
FIGURE 6-28
Trang 5FILE ENCODING
Chapter 5 showed you how to change the character and line encoding for one or more fi les using an
Info window You can alter those same settings for the fi le being edited using the View ➪ Text ➪
Line Ending and View ➪ Text ➪ File Encoding menus
Changing these settings does not, immediately, alter the actual fi le The encodings are used
to interpret the bytes in the fi le and turn those codes into Unicode characters for editing This
translation uses the encoding set for the fi le when it is read Conversely, when the fi le is later
saved, the Unicode characters in the editor are translated back into bytes using the encoding that
is set then
Problems can arise when you change from one character encoding to another Some characters
cannot be represented in certain encodings or you may have the wrong characters in the editor
because the encoding was mismatched when the fi le was read For instance, the registered trademark
symbol ( ® ) appears in the Unicode, Mac OS Roman, and Windows Latin 1 character sets, but the
binary code used to represent it in a text fi le is different for all three If the fi le was written using
Windows Latin 1 encoding and you read the fi le into the editor using Mac OS Roman encoding, the
editor displays some other symbol because the value in the fi le is not the Mac OS Roman value for
the registered trademark symbol
When you change the encoding for a
fi le, Xcode asks if it should convert or
reinterpret the characters in the fi le, as shown
in Figure 6 - 29
The Convert button actually does very little
beyond remembering the new encoding for
the fi le The characters are already in their
universal form in memory The “ conversion ”
doesn ’ t actually occur until you save the fi le The bytes in the fi le were read using the old encoding,
and will eventually be written using the new encoding Use this option if the characters in the fi le
are correct and you simply want to save the fi le using a different encoding The one thing
the Convert button does do is check that all of the characters in the fi le can be written using the
new encoding Xcode does not allow characters in an editor pane that cannot be encoded when it is
saved If the document contains characters that are illegal in the new encoding, the conversion is not
allowed
Use the Reinterpret button when the encoding is incorrect and the editor has misinterpreted the
bytes in the fi le If a fi le containing the registered trademark symbol is encoded using Windows
Latin 1, and read using Mac OS Roman, the character that appears in the fi le is “ Æ ” To correct
this, change the encoding to Western (Windows Latin 1) and click the Reinterpret button The
contents of the fi le are read again, this time using the Windows Latin 1 encoding, and the ® symbol
appears instead To save this fi le using Mac OS Roman encoding, change the encoding again — this
time using the Convert button
The Reinterpret button must re - read the bytes in the actual fi le The binary encoding of characters
is not maintained in memory — only the Unicode characters are If changes have been made
to a fi le, the Reinterpret button warns you that all unsaved changes will be lost when the fi le is
FIGURE 6-29
Trang 6reinterpreted, as shown in Figure 6 - 30 You can choose to discard all unsaved changes or cancel
To preserve and reinterpret the fi le including all of the changes that you ’ ve made, cancel the reinterpretation, save the fi le using the old encoding, and change the encoding again
As mentioned earlier, Xcode won ’ t allow you
to convert a fi le if the resulting document would have characters that cannot be encoded
Likewise, the editor won ’ t let you insert a character into a document if the fi le ’ s encoding won ’ t support it If you try, you get a warning like the one in Figure 6 - 31
FIGURE 6-30
FIGURE 6-31
You have the choice of converting your document to Unicode (really Unicode - 16) or Unicode - 8
Unicode - 16 is the “ raw ” Unicode encoding used internally by the editor Unicode - 16 writes or stores each character as a 16 - bit integer word Most editors, compilers, and other ASCII - oriented programs are not compatible with Unicode - 16 Unicode - 8 writes the common ASCII (0 – 127) characters as single bytes and encodes higher - value Unicode characters using a complex system
of escape values Unicode 8 is compact, compatible with many applications, and backwards -compatible with plain ASCII text If given this choice, convert your document to Unicode - 8
If you don ’ t want to use Unicode - 8 or Unicode - 16, cancel the insertion Convert the fi le to an encoding that supports the characters you are trying to insert, and insert them again
LOCALIZING FILES
One of the hallmarks of the Macintosh is the ability of programs to transform themselves to accommodate the language, currency, and time conventions of different countries and populations
This is generically referred to as localization Xcode supports localization of individual project fi les
It does this by maintaining multiple copies of the fi le, one for each locale
Your locale — the locale that your Xcode development system is currently set to — is called the
development region You should always start by creating the version of a fi le for your development
region When you localize a fi le, the original fi le becomes the version for the development region
You can then create variations of the fi le for other regions
Trang 7Normally, you only localize resource fi les like string lists, images, and NIB documents Localization
requires runtime support, and is usually done for fi les that are copied into the deployment bundle
The bundle is organized so that all versions are copied into the bundle and the appropriately
localized version of the fi le is loaded based on the locale of the current user Though it is possible to
localize something like a source fi le, only the development region ’ s version will be compiled
Creating Localized Versions of a File
To prepare a fi le for localization, open the Info window for the fi le ’ s item and click the Make File
Localizable button The fi le ’ s source item is turned into a group and the existing fi le becomes
the localized version for the development region In the project structure group, the source item
becomes a kind of source group The top - level item acts as a proxy for the development region ’ s
version of the fi le, equivalent to the original fi le; that is, opening the item opens the localization for
the development region To open or change settings for an individual locale, expand the group and
deal with each version separately
After you initially localize the fi le, you can add
more localizations by opening an Info window for
either the item group, or any specifi c localization,
and clicking the Add Localization button Enter the
name of the localization, or choose one from the
pop - up menu, as shown in Figure 6 - 32 A new fi le
is created by duplicating the current fi le The new
fi le becomes the localized version for that region
In the project folder, Xcode creates an .lproj
subfolder for each localization All of the fi les
localized for English are in the English.lproj
subfolder, all the fi les localized for French are
in the French.lproj subfolder, and so forth
When the fi le is fi rst localized, a localization
folder for the development region is created in
the same location, and the fi le is moved into the
.lproj subfolder The Multiglot project, shown in
Figure 6 - 33, shows the organization of the project
and project folder after the MainMenu.nib and InfoPlist.strings fi les have been localized for
English, French, and Spanish
FIGURE 6-32
Trang 8Removing Some or All Localizations
To remove a single localization of a fi le, select the specifi c localization within the fi le ’ s group and delete it like you would any other source item
To remove all localizations of a fi le, open the Info window for the localized group or any specifi c localization Click the Remove All Localizations button The localization for the development region is moved from the .lproj subfolder back to its original location The localized group in the project is replaced with a simple source fi le reference again Any additional localized versions of the fi le are left in their respective .lproj folders, but are no longer referred to in the project If
you later localize the fi le again, Xcode will not automatically pick up the other localized versions.
However, if you add a localization for a region that already has a fi le in its .lproj subfolder, the fi le
is not replaced and it becomes the localization for that region
PRINTING
Rudimentary printing of editor panes is provided through the File ➪ Print (Command + P) command The Print command prints the contents of the editor pane, using the editor ’ s font The gutter, page guide, and other annotations are not printed Lines wider than the page are always wrapped to the next line; the Wrap Lines setting of the editor pane is ignored The Use Colors When Printing option
in the Font & Colors tab of the Xcode Preferences controls whether editor and syntax coloring is applied to the printout
The text is printed at 72 dpi, which means that it will probably be unacceptably large Use the scaling feature of the page setup to reduce the size of the text A scale between 50% and 75% makes for a very readable and compact listing For an even more compact listing, set the scaling between 80% and 100% and select a two - up page layout
FIGURE 6-33
Trang 9SERVICES
Xcode, like all well - written Cocoa applications, has access to services Services are small functions
provided by other applications that can be applied to the current selection within another
application To open the URL in the source fi le shown in Figure 6 - 34, simply select it and choose the
Open URL command from the Services menu
What services you have in your Services menu depends entirely on what applications and plug - ins
you have installed
SUMMARY
As you can see, Xcode ’ s editor provides a lot of features designed to make your editing sessions fl uid
and productive Learning to use the various navigation controls will make moving around within a
fi le, and between fi les, quick and effi cient
Though the editor provides many useful features for editing and formatting your code, there ’ s
a lot more that it can do for you The next chapter describes the many syntax - aware features of
the editor
FIGURE 6-34