Save and Revert Changes to Files
As you create your app, Xcode automatically saves changes to source, project, and workspace files. This feature simplifies workflow, helps keep you from losing any of your work, and requires no configuration.
When creating an app, sometimes you may want to experiment with a new user interface layout and then revert to the previous one. Or you may need to undo some code that you’ve just written because you found an error or problem. These circumstances require that you understand how Xcode saves your work to disk and know how to revert your work to a prior state.
This chapter explains how Xcode automatically saves your work and shows how to use the Revert Document and Undo commands. The Snapshot feature is introduced as well as Xcode’s built-in interface to source code management so that you understand how they relate to the overall tasks of change management in your development projects.
Save when Editing
As you edit, Xcode continuously tracks changes and saves them, but it does not continuously write the changes to disk. They are saved in memory as you work. Xcode then automatically writes your changes to disk when one of five operations is performed:
You build and run your app.
A snapshot is created.
You commit files to an SCM repository.
You close the project.
You quit Xcode
You can also save changes to disk manually using the Save command in the File menu.
Revert to the Last Saved Version
To discard all changes you’ve made to a file since it was last saved, use the Revert Document command in the File menu. After a confirmation dialog appears, click the Revert button to complete the operation.
The Revert Document command is available only when the contents of a file have been changed since it was last saved to disk. Files with in-memory changes are indicated in two places: The file icon is highlighted in the project navigator and in the associated editor pane’s jump bar. Highlighted icons in the project navigator let you see which files have in-memory changes throughout the entire project. The highlighted icon in an editor’s jump bar indicates whether any changes to the document currently in view were made since it was last saved.
For example, in Figure 11-1 you see a workspace window with the file
MainMenu.xib open in the primary editor and three text files (
ReadMe.txt) open in assistant editors.
Three of the four files open in Figure 11-1 have changed since they were last saved, indicated by the highlighted icons in the jump bars and in the project navigator. The Revert Document command allows you to revert to the last saved version of each file, one at a time. It acts on the file that currently has the editing focus, obtained by clicking in its editor pane or by clicking its name in the project navigator. In this illustration,
MainMenu.xib has the editing focus, which you can tell by the slightly darker coloring of the jump bar in the primary editor.
Using Revert Document in Xcode always returns the contents of the file to the last saved version on disk. If you are not sure when the file was last saved to disk relative to when you made changes, or you would prefer to back out changes one at a time, use the Undo command in the Edit menu.
Undo Changes Incrementally
To back out changes to a file incrementally, use the Undo command in the Edit menu, shown in Figure 11-2.
The Undo command in Xcode allows you to incrementally back out all changes to a file since the start of an editing session. An editing session begins when you open a project with Xcode and ends when you close the project. When you work on a file in a single session, Xcode lets you undo all the edits in that session, even those already saved to disk.
When you work in a text document and choose Undo, the source editor sets the insertion point to the affected section of the text and brings that section into view. In this way, you know the change that the Undo command has undone.
Files included in a project may be locked for several reasons. For example, they may be header files included in system frameworks, or they may have been intentionally locked in the Finder to prevent accidental deletion and renaming. Locked files cannot be edited; they are available only for reading, searching, and selecting portions to copy.
If you attempt to edit the contents of a locked file, Xcode presents a notification asking whether you want to unlock the file. Click the Unlock button to continue the unlocking operation. If the file cannot be unlocked, you receive another notification (see, for example, Figure 11-3).
Locked files are indicated in the Xcode workspace by a locked file icon in the project navigator or by a lock icon in the associated editor pane’s jump bar. The locked file icon also appears in the workspace window title when the document is in the primary editor pane (see Figure 11-4).
When you start editing a locked file, Xcode asks whether you want to unlock it. You can also unlock a file by using the Unlock command in the File menu or by clicking on the lock icon in the jump bar of its associated editor pane.
Use Snapshots and SCM for Projectwide Change
When you develop a project, you often make changes on a larger scale than changes to individual files. The snapshot and source code management systems provide save and revert facilities that operate project wide.
Snapshots Archive Projectwide Changes to Documents and Configuration
Use the Create Snapshot command in the File menu to create an archive of the project or workspace.
You can make three types of projectwide changes for which the Revert Document and Undo commands are not supported:
Xcode projectwide operations, which involve changes to many document files and potentially to project settings. These operations include refactoring code, adding Automatic Reference Counting to an existing project, and performing project validation.
Adjustments to the workspace and project settings. Xcode saves these changes to disk as they are performed by modifying the
Your global editing operations using search and replace functions.
Snapshots are archives that include the current state of all project and workspace settings and all document files included in the project context. All document files are saved when a snapshot is created. Xcode always stores snapshots in your local file system.
Creating a snapshot before making changes to project settings, and before using global search and replace, gives you the ability to revert the entire project to the state it was in before you performed a projectwide operation.
For more information about snapshots, see “Save and Revert Changes to Projects.”
SCM Repositories Manage Overall Change as Your Project Develops
The Source Control commands in the File menu allow you to manage your project files with a source code management (SCM) repository.
Automatic save, revert, undo, and snapshots are intended to help the flow of editing and making adjustments as you develop your project with respect to the document files it includes. These features are not intended to be the way you manage overall project change through the development history of a project. To support this larger scope of managing change in your development work, Xcode provides built-in access to source code management commands that allow you to use both locally stored and server-based SCM repositories. Using an SCM repository is strongly recommended even for small development projects.
For more information about using source code management, see “Save and Revert Changes to Projects.”
© 2013 Apple Inc. All Rights Reserved. (Last updated: 2013-04-23)