Monday 4 March 2013

Xcode Tips and Tricks


Switch to Header/Source File

  • Option ⌥ Command ⌘ Up Arrow ↑
  • View > Switch to Header/Source File
Switches between the .m and .h files.
  • In Xcode 4 this is ctrl Command ⌘ Up Arrow ↑


⌘ Command + Double-Click on a symbol: Jump to Definition of a symbol.
⌥ Option + Double-Click on a symbol: Find Text in Documentation of a symbol. (Only works if you have they symbol's Doc Set installed.)

Favorites Bar:

Favorites bar is just like you have in Safari for storing - well - favorites. I often use it as a place to store shortcuts (which you can do by drag-dropping) to files I am using right now. Generally this is more useful when I'm working with a large or unfamiliar project.
To show the Favorites Bar, select the following menu option:
  • View > Layout > Show Favorites Bar

Auto-completion Keyboard Shortcuts

Tab ⇥ OR Control ⌃ /: Select the next auto-completion argument.
Shift ⇧ Tab ⇥ OR Shift ⇧ Control ⌃ /: Select the previous auto-completion argument.
Escape ⎋: Shows the auto completion pop-up list.

Turn off the "undo past the last point" warning

When you attempt to undo after saving, you will get the following prompt:
"You are about to undo past the last point this file was saved. Do you want to do this?"
To get rid of this warning, enter the following into a terminal window:
defaults write com.apple.Xcode XCShowUndoPastSaveWarning NO

Change the company name in template files

Paste this into the Terminal application:
defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "Microsoft";}'

Change "com.yourcompanyname" in all your templates:

  1. Find the directory:/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application
  2. Use your favourite multi-file search-and-replace tool to change com.yourcompany to whatever value you normally use to build for a device. I used BBEdit's multi-find-and-replace after I opened the whole directory. You should be replacing the value in all the info.plist files. I found 8 files to change. The number of times a build has failed because I forgot to change this string is ridiculous.

Quickly jump to a Group in the Groups and Files pane

  • Control ⌃ Option ⌥ Shift ⇧ + <First letter of a Group name>
If you hold down the three keys above, you can quickly jump to groups in the left (Groups and Files) page by pressing the first letter of a groups name. For example, Control ⌃Option ⌥Shift ⇧T takes you to Targets and Control ⌃Option ⌥Shift ⇧S to Source. Press it again and it jumps to SCM. Sometimes it takes several tries for this to work (I don't know why).

Cycling between autocompletion choices

  • Control ⌃ .
  • Shift ⇧ Control ⌃ .: Cycles backwards between autocompletion choices.
Control ⌃. (Control-Period) after a word automatically accepts the first choice from the autocompletion menu. Try typing log then Control ⌃. and you'll get a nice NSLog statement. Press it again to cycle through any choices. To see all the mutable choices, type NSMu then Control ⌃..

Quick Help

  • Control ⌃ Command ⌘ ? (While your cursor is in the symbol to look up)
  • Option ⌥ + <Double-click a symbol>
  • Help > Quick Help
To get to the documentation from the Quick Help window, click the book icon on the top right.

See the documentation for a symbol

  • Command ⌘ Option ⌥ + <Double-click a symbol>
Takes you straight to the full documentation.

Make non-adjacent text selections

  • Command ⌘ Control ⌃ + <Double-click in the editor>
Use the above shortcut for a strange way of selecting multiple words. You can make selections of words in totally different places, then delete or copy them all at once. Not sure if this is useful. It's Xcode only as far as I can tell.

Use Emacs key bindings to navigate through your code

This trick works in all Cocoa application on the Mac (TextEdit, Mail, etc.) and is possibly one of the most useful things to know.
  • Command ⌘ Left Arrow or Command ⌘ Right Arrow Takes you to the beginning and end of a line.
  • Control ^ a and Control ^ e Do the same thing
  • Control ^ n and Control ^ p Move the cursor up or down one line.
  • Control ^ f and Control ^ b Move the cursor back or forward one space
Pressing Shift ⇧ with any of these selects the text between move points. Put the cursor in the middle of a line and press Shift ⇧ Control ^ e and you can select to the end of the line.
Pressing Option ⌥ will let you navigate words with the keyboard. Option ⌥ Control ^ f skips to the end of the current word. Option ⌥ Control ^ b skips to the beginning of the current word. You can also use Option ⌥ with the left and right arrow keys to move one-word-at-a-time.
  • Control ^ Left Arrow and Control ^ Right Arrow moves the cursor between camel-cased parts of a word.
Try it with NSMutableArray. You can quickly change it to NSArray by putting your cursor after theNS, pressing Shift ⇧ Control ^ Right Arrow then Delete.

Open Quickly...

  • Command ⌘ Shift ⇧ D
  • File > Open Quickly...
I'm a big fan of the Open Quickly feature, which is particularly good in Xcode 3.1 and later. When you want to open a file or a symbol definition that's in your project or in a framework, just hit the keyboard shortcut, type a bit of the file or symbol's name, use Up Arrow ↑ and Down Arrow ↓ to pick to the right result (if need be), and then hit Return ↩ to open the file or navigate to the symbol definition.
On Xcode 4:
  • Command ⌘ Shift ⇧ o

Open Quickly uses the current word as a search term

Also, something I didn't know about Xcode until two minutes ago (when schwa pointed it out in a comment) is that, if the editor's text caret is inside of a word when Open Quickly is invoked, that word will be used as the Open Quickly search term.

Use #pragma for organization

You can use:
#pragma mark Foo
... as a way to organize methods in your source files. When browsing symbols via the pop up menu, whatever you place in Foo will appear bold in the list.
To display a separator (i.e. horizontal line), use:
#pragma mark -
It's very useful, especially for grouping together delegate methods or other groups of methods.

Breakpoint on "objc_exception_throw"

You should always have a breakpoint on objc_exception_throw.

Debugging retain/release problems with "Zombie" variables

Use the following code:
NSZombieEnabled = YES;
NSDeallocateZombies = NO;
... to debug retain and release problems. For more information, see the "Finding Memory Leaks" section of Apple's Debugging Applications document.

Jumping to a class in Xcode from Interface Builder

Command ⌘ + Double-click on an object in Interface Builder's Document Window to jump to that class in Xcode. This is very handy with File's Owner.

Reusing customized objects in Interface Builder

Drag a customized object back to Interface Builder's Library for later reuse.

Select overlapping items in Interface Builder

Control ⌃ Shift ⇧ + Click on an object in Interface Builder to see a menu of all of the objects under the mouse.

Interface Builder Gesture Guide

Zoom Editor In

If your window displays both the detail and editor view, you can zoom the editor in to expand the editor view to the full height of the window. (This is fairly easily found, but many seem to overlook it.)
You can do this by using one of the following methods:
  • Command ⌘ Shift ⇧ E
  • View > Zoom Editor In
  • Drag the splitter (between the editor window and the file list above it) upwards.




KeyBoard Shortcut List 


Get Colin Wheeler's Complete Xcode Keyboard Shortcut List (available as PDF or PNG). Print it and keep it somewhere visible (I've got it on the wall next to my screen).

Control Xcode's text editor from the command line: xed
> xed -x                # open a new untitled document
> xed -xc foo.txt       # create foo.txt and open it
> xed -l 2000 foo.txt   # open foo.txt and go to line 2000

# set Xcode to be your EDITOR for command line tools
# e.g. for subversion commit
> echo 'export EDITOR="xed -wcx"' >> ~/.profile

> man xed               # there's a man page, too

With Trackpad:
  • Swipe Three Fingers Up - Switch between header and source file, which is easier than Cmd + Opt + Up;
  • Swipe three fingers down - Switch between declaration and definition when selecting a class or method, found these two kind currently;
  • Swipe three fingers left - Go back (Cmd + Opt + Left);
  • Swipe three fingers right - Go forward (Cmd + Opt + Right);
Tested with Xcode 3.2.5.

No comments:

Post a Comment