11

In Xcode 4, I can press Ctrl-6 to get a list of all the methods in the current file.

The problem is, if I have private methods declared at the top of my implementation file, say:

@interface Foo ()

-(void)tap:(id)sender;

@end

@implementation Foo

...

-(void)tap:(id)sender
{
  ...
}

then starting to type "tap" while the method list is visible will just take me to the declaration, since it comes first in the file, when what I really want is the implementation.

Is there any way to exclude these declarations from the method list or do I need to resort to separate Foo.h and Foo+Private.h headers?

Thanks!

Bill
  • 44,502
  • 24
  • 122
  • 213
  • Great question. Wish I had an answer for you. How did you learn about CTRL-6? As a long-time Eclipse user, I'm constantly frustrated by the lack of keyboard-shortcut support in XCode – curthipster Mar 26 '11 at 05:38
  • @colbeerhey http://www.1729.us/xcode/Xcode%20Shortcuts.png is a good summary, although it doesn't include Ctrl-6. I knew that Ctrl-2 did the same thing in Xcode 3, so I just tried various Ctrl-n in Xcode 4 until I found something that worked. – Bill Mar 26 '11 at 18:19
  • That's the Show Document Items command. If you go to View->Editor you'll see a list of related commands and their shortcuts. You can customize the shortcuts as much as you like in the Key Bindings section of Xcode's preferences. – Caleb Mar 28 '11 at 01:38

3 Answers3

5

You dont need to declare you private methods and you wont get a warning by default anymore. So one options is not to declare a prototype at all.

Otherwise as curthipster mentioned ctrl-6 is a good shortcut. I use this all the time (no mouse needed):

  1. Press ctrl-6
  2. Type the first few letter of the method name (or a word it contains and you dont even have to spell it spot on, the search filter is very good!)
  3. Tap down until the method is selected.
  4. Tap enter

Alternativly open the assistant with cmd-alt enter (to close use cmd-enter, see more shortcuts here). You can make the assistant editor look at the same file, so it has one part of view at the top and one at the bottom for example.

Community
  • 1
  • 1
Robert
  • 37,670
  • 37
  • 171
  • 213
4

I don't think there's a way to exclude the method declarations from the Document Items popup.

If you get in the habit of using code folding, however, you might not rely so much on that popup to navigate your source code. There are commands for folding both methods and comment blocks, and you can fold all methods with one quick shortcut (command-option-shift-left arrow to fold, -right arrow to unfold by default, though you can of course customize the keys). See the Editor->Code Folding submenu for a complete list of related commands.

When you fold all comments and methods in a .m file, almost all you're left with is a list of methods that's makes it easy to find what you're looking for. You can then unfold just that method or all methods with another keystroke. It's a little strange to see all your code disappear when you first start using folding, but it's a very handy feature.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    That's a good point - I'll give code folding a try and see if I miss Ctrl-6. Thanks for the tip! – Bill Mar 28 '11 at 13:02
  • Glad to help. Don't forget that you can jump straight to a method's definition if you see the method in code somewhere. You can control-click and Jump to Definition, or just put your cursor on the method and use the shortcut, which is command-control-D by default. – Caleb Mar 28 '11 at 13:09
1

Usually, it's better to add a named category for the private methods:

#import "Foo.h"

@interface Foo( Private )
    - ( void )tap: ( id )sender;
@end

@implementation Foo( Private )
    - ( void )tap: ( id )sender
    {}
@end

@implementation Foo
    ...
@end

Then you'll see each one. It may not answer your question, but at least you'll see your method.

One thing is also to organize your methods with mark pragmas:

#pragma mark - Private methods

May help you to navigate through the completion dialog... Hope this helps...

Robert
  • 37,670
  • 37
  • 171
  • 213
Macmade
  • 52,708
  • 13
  • 106
  • 123
  • On the other hand, you won't get compiler warnings ("Foo does not respond to tap:") anymore if you forget to implement a method of the category (this is one of the benefits of using "empty" categories). – goetz Mar 28 '11 at 11:51