Xcode Reference
Kip Landergren
(Updated: )
My cheat sheet for Xcode covering common keyboard shortcuts, file templates, snippets, and comments.
Contents
- Xcode Version Note
- macOS Keyboard Shortcuts
- Debugger
- Xcode File Templates
- Xcode Project Templates
- Xcode Snippets
- Swift Package Management
- Swift Comments and Xcode
- Xcode Terminology
- FAQ
- Resources
Xcode Version Note
The following document refers to Xcode version 11.4.
macOS Keyboard Shortcuts
Note: all letter keys are displayed uppercase. Shortcuts requiring use of ⇧ Shift will be called out.
Fast Menu Item Access
Not every operation has a keyboard shortcut. In the event that you know the title of the menu item you wish to select you can use the following steps to quickly access it:
access Help menu, focusing search | ⌘ Command + ⇧ Shift + / |
access Menu Item | type Menu Item and select from search results |
Xcode
move cursor page up | ⌥ Option + Page Up |
move cursor page down | ⌥ Option + Page Down |
reindent code | ⌃ Control + I |
focus Active Scheme | ⌃ Control + 0 |
focus Document Items | ⌃ Control + 6 |
focus Project navigator in the Navigator area | ⌘ Command + 1 |
focus Find navigator in the Navigator area | ⌘ Command + 4 |
move cursor to end of line | ⌘ Command + → |
move cursor to begining of statement; second invocation moves cursor to beginning of line | ⌘ Command + ← |
move cursor to top of file | ⌘ Command + ↑ |
move cursor to bottom of file | ⌘ Command + ↓ |
find | ⌘ Command + F |
focus Editor area | ⌘ Command + J |
test | ⌘ Command + U |
jump to next issue | ⌘ Command + ' |
add breakpoint / remove breakpoint | ⌘ Command + </kbd> |
rename in scope | ⌘ Command + ⌃ Control + E |
jump back to location | ⌘ Command + ⌃ Control + ← |
jump forward to location (does not wrap) | ⌘ Command + ⌃ Control + → |
clean build folder | ⌘ Command + ⇧ Shift + K |
show the snippets library | ⌘ Command + ⇧ Shift + L |
open quickly | ⌘ Command + ⇧ Shift + O |
show / hide the debug area | ⌘ Command + ⇧ Shift + Y |
jump to previous issue | ⌘ Command + ⇧ Shift + " |
find and replace | ⌘ Command + ⌥ Option + F |
fold | ⌘ Command + ⌥ Option + ← |
unfold | ⌘ Command + ⌥ Option + → |
automatically fix next issue | ⌘ Command + ⌥ Option + ⌃ Control + F |
rerun last test | ⌘ Command + ⌥ Option + ⌃ Control + G |
run single test in cursor context (note: I have found this to be flaky; trying to rerun last test sometimes works) | ⌘ Command + ⌥ Option + ⌃ Control + U |
switch destination device “down” | ⌘ Command + ⌥ Option + ⌃ Control + [ |
switch destination device “up” | ⌘ Command + ⌥ Option + ⌃ Control + ] |
fold all functions | ⌘ Command + ⌥ Option + ⇧ Shift + ← |
unfold all functions | ⌘ Command + ⌥ Option + ⇧ Shift + → |
Menu Command Shortcuts (By Menu) - Documentation Archive
Playgrounds
run playground | ⌘ Command + ⇧ Shift + ↵ Return |
run playground to current line | ⇧ Shift + ↵ Return |
Simulator
shake gesture | ⌘ Command + ⌃ Control + Z |
Turning on Dark Mode
Within the running simulator itself:
- open Settings app
- select Developer
- toggle Dark Appearance
Through Xcode, when app is running:
- select Environment Overrides
- toggle Interface Style
- select Dark
Note: this latter method does not appear to take effect during UI tests.
Viewing Application Data
xcrun simctl get_app_container booted <app bundle identifier> [<container>]
Get application data directory:
xcrun simctl get_app_container booted <app bundle identifier> data
Debugger
Set language to swift:
(lldb) settings set target.language swift
Import UIKit
:
(lldb) e import UIKit
Assign memory address to usable type:
e let $foo = unsafeBitCast(0xMEMORY_ADDRESS, to: FooType.self)
Xcode File Templates
Text Macros
___FILEHEADER___ |
the default author, project name, date, and copyright text |
___FILEBASENAME___ |
the input filename, without extension |
___UUID___ |
a universally unique identifier |
A complete list is available from the official Xcode Text macros reference help article.
Editor Placeholders
Special text that fills a selectable region:
<#My Placeholder Text#>
Xcode Project Templates
This undocumented (at least as of March 2020) feature that allows you to create your own custom project templates similar to the Single View App already available.
The general problem of how to consistently create Xcode projects and project boilerplate is addressed in a few primary ways:
- create a default project, and rename it
- use a project generator
- create custom Xcode Project Templates
- follow a process
Although using custom templates is possible, I prefer to rely on a manual process because at the moment, my needs are not complex.
In addition, this allows me to:
- roll better with Xcode changes
- reduce dependency on 3rd party code
- reduce maintenance and support
If this Xcode feature becomes better supported in future, or a separate need arises, I will reevaluate.
Xcode Snippets
User-created snippets are stored as .codesnippet
plist XML files in:
~/Library/Developer/Xcode/UserData/CodeSnippets
Considerations when creating:
- text macros (e.g.
___FILEBASENAME___
) are not supported - editor placeholders (e.g.
<#My Placeholder Text#>
) are supported - nested editor placeholders are not supported
- avoid dashes (
-
) inCompletion
values to avoid evaluation as expression - prefer underscores (
_
) asCompletion
separators - prefer common prefix for easy reference
- multiple scopes may be selected
Official Xcode Create and use code snippets help article.
Swift Package Management
Swift Comments and Xcode
Horizontal Delimiter
// MARK: -
creates a solitary horizontal delimiter in the Xcode jump bar and minimap
Headings
// MARK: Delegate Methods
creates a heading with text “Delegate Methods” in the Xcode jump bar and minimap
// MARK: - Delegate Methods
creates a horizontal delimiter, followed by a heading with text “Delegate Methods” in the Xcode jump bar and minimap
Action Items
Note: As far as I can tell there is no additional management of action items beyond special formatting in the jump bar
// TODO: finish implementing delegate methods
creates a to-do item with text “finish implementing delegate methods” in the Xcode jump bar
// FIXME: broken on horizontally compact
creates a fix-me item with text “broken on horizontally compact” in the Xcode jump bar
Xcode Terminology
- destination
- the architecture of the hardware for which the products are built
- library
- a collection of code or data that can be imported
- product
- the output of a build system, e.g. a module or test suite
- scheme
- either a collection of targets to build, a configuration to use when building, or a collection of tests to execute
- target
- two things: the desired product of a build and the set of source files and instructions to actually build it
FAQ
How to switch to an older iOS version for testing?
Create a simulator configuration via these steps.
How to perform a global find and replace?
A project-wide find and replace is possible by:
- focusing the Find Navigator
- clicking Find
- selecting Replace
And using as needed.
How to fix a broken Xcode index?
Delete derived data and restart xcode.
Where is the derived data directory located?
In ~/Library/Developer/Xcode/DerivedData.
Or, from within Xcode:
- open Preferences
- select Locations
- view Derived Data information there
Resources
- Official Xcode Help
- Menu Command Shortcuts (By Menu) - Documentation Archive
- Xcode Keyboard Shortcuts (PDF) by Swift Education
- Debugging with Xcode - Apple Documentation Archive
- Xcode Release Notes
- Xcode Release Notes — Archive