109

Now that Xcode 4 is officially released it's time for a follow up to my previous question: Hidden Features of Xcode

So what are they? What are those hidden little Xcode 4 hints & tips that you can't live without?

Please limit your tips to those that are new and specific to Xcode 4!

Community
  • 1
  • 1
schwa
  • 11,962
  • 14
  • 43
  • 54

23 Answers23

42

If you like your code to look as good as it runs, you've undoubtedly used #pragma mark - and #pragma mark <name> to provide a nice visual grouping in the Xcode class dropdown list. Xcode 4 now combines these into a single #pragma mark - <name>.

More on pragma mark.

Xcode pragma mark

memmons
  • 40,222
  • 21
  • 149
  • 183
35
  • shift-cmd-opt click on a symbol lets you tell Xcode exactly where in the UI you want the navigated-to file to open.

  • cmd-ret makes the version editor / assistant (the right hand pane) go away

  • shift-cmd-Y to hide the debugger

  • cmd-0 to hide the navigator (left pane)

  • cmd-[1..n] switch between navigators on left

  • cmd-opt-[1..n] switch between utilities (thing generally on right)

  • ctrl-[1..n] switch between various related files in the editor

Overall, Xcode 4 tries to keep things consistent w/the hot keys. Cmd is for mainline commands. Adding modifiers is for analogous commands focused on one particular sub-feature.

(These are just the four that come to mind as the my most pounded upon shortcuts that I'm using constantly! I'm sure it'll change over time as my workflow is refactored into the new hotness.)

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
bbum
  • 162,346
  • 23
  • 271
  • 359
  • 8
    shift-cmd-opt-click... Mind. Blown. Thank you. – Danilo Campos Mar 23 '11 at 21:41
  • 1
    Oooh... thanks for editing that, Dave. Didn't know about .... – bbum Mar 23 '11 at 22:14
  • 10
    The ⇧⌘⌥-click thing is a fusion of two shortcuts: ⌘-click on any symbol to open the file that declares it, and ⇧⌥ to bring up the where-to-open-it pop-over. You can use the latter key-combo augmentation on a couple of other things, as I described in one of my answers. – Peter Hosey Mar 23 '11 at 23:05
  • 3
    Also, for those like me who used Interface Builder's ⌘-[1–6] shortcuts for the different Inspectors, those became the ⌘⌥-[1–7] you mentioned, when editing a nib. – Peter Hosey Mar 23 '11 at 23:06
  • Is there a way to use ⇧⌘⌥-click without actually using a mouse, but keyboard instead (and when caret is on a symbol you want to open)? ⌘⌥-, opens a new adjacent editor, but AFAICS only current file is considered. – Piotr Byzia Mar 30 '11 at 10:25
  • ⇧⌘⌥-ret - show a visual diff of the current file. I wanted to explicitly add this one since the new integrated diff viewer is one of my favorite improvements. The time machine-like slider is a nice added touch. – Sebastien Martin Apr 10 '11 at 19:56
  • Is there a way to hide all but the main editor window...? Essentially one command for Cmd + Return, Cmd + 0, Cmd + Shift + Y – uncaught_exceptions Oct 28 '11 at 05:03
18

Xcode 4 adds a new "Code Snippets" feature, accessible via View > Utilities > Code Snippet Library. To add a new code snippet, select some text in the editor view and drag it into the snippet library. You can either drag a snippet out and drop it in your code to use it, or, much more conveniently, assign a completion shortcut to it.

When you type in that series of letters, it will automatically show up in the code completion pop-up menu as you type your code. For example, among many others, I have a snippet set up so that typing "svk" will expand to setValue:<#value#> forKey:<#key#>. This can save a lot of typing and/or fishing around in the autocompletion menu for the methods and other code expressions you use the most. Xcode ships with a bunch of built-in snippets that come in very handy as well.

Brian Webster
  • 11,915
  • 4
  • 44
  • 58
15

New shortcut I use most often: triple-finger swipe (up or down) to swap counterparts (between the interface and implementation files). No more hand acrobatics!

Thanks to https://twitter.com/xctotd/status/48148271759241216

wdn
  • 191
  • 1
  • 5
  • 1
    @Dave DeLong, how can you do this on the keyboard? I was doing option-command-up to get from interface to implementation in XCode 3. This no longer works in 4. What's the new way? – Dan Rosenstark Mar 31 '11 at 18:17
  • 5
    @yar control-cmd-up/down arrow – Dave DeLong Apr 01 '11 at 03:20
  • Thanks as always, @Dave DeLong. – Dan Rosenstark Apr 01 '11 at 13:59
  • Is this still the case in Lion? Cos in lion a 3 finger vertical swipe on the trackpad activates Mission Control. I'm assuming we're talking about trackpad swiping and not the magic mouse. – DonnaLea Aug 04 '11 at 07:53
  • Doesn't seem like there's a simple solution currently, even if you switch Mission Control to a 4-finger swipe. See this thread: http://stackoverflow.com/questions/6771057/trackpad-gesture-to-switch-to-header-source – wdn Aug 05 '11 at 14:56
  • @wdn It’s four fingers in Lion. – mxcl Oct 21 '11 at 09:31
13

shift-option-click on a file in the Groups and Files list Project Navigator, or hold shift-option while choosing a file from the Jump Bar.

You'll get a pop-over from which to pick where to open the file. New or existing tab, new or existing split (Assistant) editor, new window—every possible option is there.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • 4
    Thanks for pointing this out. Very useful. It also works on the selected file in the 'Open Quickly' dialogue (⇧⌘O). – Cris Mar 24 '11 at 06:06
  • 2
    I love how so much work's gone into a little UI widget that most people will never see. :) – Simon Whitaker Mar 31 '11 at 21:10
13

Tabs in Xcode 4 are awesome. Each one maintains its own separate layout, so you can have multiple tabs with different things in them and have each one lain out appropriately.

I've been setting up my projects with three layouts—one for the project object, one for each nib (showing the owner's class's sources), and one for each class.

There is one bug here: As of 4.0, Xcode will reset the state (turning off any Assistants) of the active tab when you reopen the project. The workaround is to switch to a tab with no Assistants for Xcode to close before you close the project. I have filed this as Radar number 9178441.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
10
  • ctrl-6 pops down within-file segment of jump bar (enables keyboard-controlled jump to methods)

  • cmd-{/} select previous / next tab

  • cmd-J navigate to any editor (but why oh why doesn't TAB then cycle between them?)

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Cris
  • 1,939
  • 3
  • 23
  • 37
  • 1
    Also, note that keyboard shortcuts are configurable. I changed previous/next tab to ⌘⌥⇠/⇢ to match Chrome. – Peter Hosey Mar 23 '11 at 22:52
7

Editor ShortCuts

  • Cmd + Ctrl + up/down Jump between .h & .m Files.
  • Cmd + Ctrl + Left/Right Jump between Previous Edited Files/Next Edited files.

Layout Shortcuts

  • Cmd + 0 hide or show navigators
  • Cmd + opt + 0 hide or show inspectors
  • Cmd + Shift + Y hide or show debugger console.

Navigators Shortcuts

  • Cmd + 1 Project
  • Cmd + 2 Symbol
  • Cmd + 3 Search
  • Cmd + 4 Issue
  • Cmd + 5 Debug
  • Cmd + 6 BreakPoint
  • Cmd + 7 Log

Tab Switching & Jumping

  • Cmd + { Previous Tab
  • Cmd + } Next Tab
  • Cmd + j Select location to jump
  • Shift + Option + ClickFile Select location to open file
  • Cmd + Alt + Option + Click Select location to open Method

Inspectors

  • Cmd + Opt + 1 File Inspector
  • Cmd + Opt + 2 Quick Help
  • Cmd + Opt + 3 Identity Inspector
  • Cmd + Opt + 4 Attribute Inspector
  • Cmd + Opt + 5 Size inspector
  • Cmd + Opt + 6 Connection Inspector

Build Related

  • Cmd + B Build
  • Cmd + Shift + K Clean
  • Cmd + . top

Code Folding

  • Cmd + Alt + Left Minimize Current Method/Class/Interface
  • Cmd + Alt + Right Expand Current Method/Class/Interface
  • Cmd + Alt + Shift + Left Minimize All Method/Class/Interface
  • Cmd + Alt + Shift + Right Expand All Method/Class/Interface
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
6

Here's the first. Tabs! Xcode 4 has replaced Xcode 3's useful but rarely used "favorites bar" with persistent tabs. This means you can keep a working set of commonly used documents in your tab bar.

Not exactly a hidden feature as such - but the fact that the tabs are persistent is perhaps easily missed.

schwa
  • 11,962
  • 14
  • 43
  • 54
  • 2
    Also, that you can assign custom names to the tabs, which was something I didn't know until Scott Stevenson mentioned it: https://twitter.com/scottstevenson/status/50270610038865921 – Brad Larson Mar 23 '11 at 19:03
  • 5
    Yep, You can double-click tabs in Xcode 4 to rename them, then use named tabs in Prefs > Behaviors (via @scottstevenson‎) – cocoapriest Mar 23 '11 at 19:04
  • 3
    Oh, this is very handy. So I can set up a debugger/console tab showing only those, then have that show up during a run. This is much nicer than having it popping up and down all the time. – Danilo Campos Mar 23 '11 at 23:43
6

Some useful hotkeys for working on smaller screens are:

  • cmd-shift-y - Hide and show the console/debug area

  • cmd-0 - Hide and show the file navigation

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
aitskovi
  • 83
  • 1
  • 7
6

If you're having any sort of trouble with the indexing of your project (e.g. symbols not showing up in autocompletion), open the Organizer window, click the Projects tab, and use the "Delete" button to delete the derived data for your project. This will force a rebuild of the project's index, as well as tossing out any built products.

Brian Webster
  • 11,915
  • 4
  • 44
  • 58
6

You can now associate groups in the Project Navigator with folders on disk. It always bugged me before that the Xcode 3 file view may not represent the actual structure of the project on disk.

Create a new group, then with the new group highlighted, open the File Inspector. In the identity section on the right just below the 'Path' is a small icon, click this to open the file browser dialog allowing you to select the directory for this group.

File Inspector

Patrick
  • 868
  • 8
  • 23
5

If you hold the FN key then you can scan through your document with the cursorkeys, without moving the cursor

Toad
  • 15,593
  • 16
  • 82
  • 128
5

Jump bar:

if your write outside function:

// FIXME: A Bug
// TODO: Later
// ???: ???
// !!!: !!!
// MARK: This a mark

Will like:

Also you can search:

Click with Option(With Shift at the same time, you can choice another window), you can see a dialog letting you specify where the file should open:

Source control:

If you use svn or git, discard change is easy:

Interface buidler

In xcode 4.2, you can jump to interface builder here:

Click a ui object with Shift, it will show a list of all objects under the point.

Try this: Select a ui object, hold Option, then move mouse over other objects. It will like:

Other

Hotkey: Shift+Command+O Open Quickly

Try:

#error message
#warning message

And read: Writing and Editing Source Code

BB9z
  • 2,432
  • 1
  • 30
  • 36
4

Text Editor:

  • Cmd-clicking on a class name will take you to the implementation (or header) file of that class, cmd-clicking on an object will take you to where the object is declared.
  • Alt-clicking will open a documenation popup for the object you clicked on.

Adding frameworks: You cannot simply do this by right-clicking on the project or a group (like in Xcode 3). You need to go to the project view, select your target and go to "Build Phases" tab. Frameworks can be managed under the "Link Binary with Libraries" section.

Setting target dependencies: Similar to the above, in the "Target Dependencies" section of the "Build Phases" tab.

Keep your archives safe -prior to installing Xcode 4 (from 3). You will lose them (most likely) and with them lose your ability to symbolicate your crash logs.

Delete action-BEWARE: "Delete" (instead of "Remove Reference Only") directly erases the file from your drive. It doesn't go into the Trash, so you cannot recover it.

Nick Toumpelis
  • 2,717
  • 22
  • 38
4

cmd-J jumps to the editor, but if you have more than one editor open you get an overlay that has all of the open editors. you can use cmd- and cmd- to switch between the open editors.

Also, if you are having a hard time finding an option or property for anything the utilites window is probably your best bet:

hide and show utilities: cmd-opt-0

Things that are commonly hidden here that aren't intuitive:

  • data model entity properties.
  • data model properties
  • xib properties
Jon
  • 462
  • 4
  • 13
4
  • You don't need to declare IBAction - methods in .h File. & You can directly connect it in Interface Builder.

Example:

-(IBAction)btnLogInTapped:(id)sender {
    // place following method in .m file of your code. save file. Open the
    // .xib file of viewController
    // click on files owner
    // click on connection-inspector.
    // you can view your methods under "received actions"
}
  • You don't need to declare variables, if you are creating property for it.

Example:

// a property declared in .h File of your view controller.
@property (nonatomic, retain) LogInVCtr *nxtLogInVCtr;

// synthesize above property in .m File
@synthesize nxtLogInVCtr;
// or
@synthesize nxtLogInVCtr = _nxtLogInVCtr;
// and you are done, you don't need to declare variable for it.
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
3

One of my favorites is the "Open Tab" behavior preference. If you go to "Behaviors" in Preferences, you can set it to go to a specific tab whenever a run starts. You can name the tab, something like "Debug" and Xcode4 will create it for you if it doesn't exist, and take you there when you start debugging. You can configure this tab just how you like it for debugging sessions, and it will stick that way after you close it, so whenever you do a run Xcode4 will be set up just how you want it, in a separate tab. You can even tear this tab off to a separate window, in which case Xcode4 will open a new specially configured "Debug" window each time you start a run! This feature is great because it keeps your regular code editing tab or window uncluttered with the log and debugger panes. Simply close the "Debug" window when you're done running and you can be back to your clean screen of code.

Michael Gorbach
  • 534
  • 2
  • 4
2

In XCode3, There was an option for "Rename the Project". Now, you just need to click twice ( second click slowly ) to rename the project & Its done.

sagarkothari
  • 24,520
  • 50
  • 165
  • 235
2

To Limit the Number of Concurrent Build Operations

This is a (somewhat) hidden preference which allows you to define the number of concurrent compile tasks:

defaults write com.apple.dt.Xcode IDEBuildOperationMaxNumberOfConcurrentCompileTasks 4

To Disable Indexing in the IDE

This one disables code sense indexing, which can save a lot of resources in large projects:

defaults write com.apple.dt.XCode IDEIndexDisable 1

to reenable:

defaults delete com.apple.dt.XCode IDEIndexDisable

Note that you should quit Xcode, set the preference values as desired, changes take effect when you restart Xcode.

justin
  • 104,054
  • 14
  • 179
  • 226
1

If you prefer using vim/emacs running on, say, Terminal, or any other external editor, you can just create your xcode project, add files and whatnot and then switch to editing said files using whichever edit suits you best; whenever you switch focus to an xcode4 window, the modified files will be reloaded( was also the case with Xcode3 ) and you can build / run your application.

1

Two kinds of Build location specification using XCode4 Preferences.

  1. Location Specified By Targets - 'Build' Directory is located within your project directory.
  2. Location in Derived Data ( Recommended by XCode, as you can see in screen shot ) - 'Build' Directory is located under DerivedData location specified ( e.g. here in my case, /Users/digicorp/Library/Developer/XCode/DerivedData )

Please look at following screen shots.

enter image description here

enter image description here

sagarkothari
  • 24,520
  • 50
  • 165
  • 235
0

QUICK HELP

Ctrl + Option / Alt + f

Find the Quick help. For example,

@property (nonatomic, retain) NSString *strQuery;

My Text cursor is at NSString & if I press Ctrl + Option + f then It will show a quick documentation in the inspector/utilities.

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
sagarkothari
  • 24,520
  • 50
  • 165
  • 235