47
votes

Coming from an Eclipse / Java background, one of my favorite features is the ability to quickly stub out all the methods required by an interface. In Eclipse, I can choose 'Override / implement' from the source menu to generate stub methods for any method of the Interface.

I'd like to do the same thing in Objective-C. For instance, if I declare a class that implements the 'NSCoding' protocol, I'd like to have Xcode automatically generate the methods required to implement this Protocol. It's frustrating to have to look-up and then copy/paste the signatures of the required methods every Protocol that I'm trying to implement.

I've been trying for awhile to find out if this is possible, but haven't found anything promising yet. Is this possible in XCode?

10

10 Answers

8
votes

I believe that Accessorizer will do what you want.

7
votes

Accessorizer will write the encode and decode methods for ivars passed to it (NSCoding protocol and for NSDocument archiving). It will also generate string constants either static or #define with a custom prefix; copyWithZone:; and other things if you need - all from a simple shortcut via Services or from the toolbar. Accessorizer keyed archiving

6
votes

Not the direсt answer, just hint:

Out of the box XCode can't.

But AppCode can.

It can do this things automatically (with your permission, of course).

If some methods of protocol marked as @required - AppCode will highlight the implementation and suggest to implement this methods.

example

@optional methods also available to implement automatically (shortcut: control + I).

1
votes

Your can create scripts for the scripting menu item in AppleScript, Perl, Python, Ruby, or any other scripting language that go in the scripting menu.

Your could place the insertion point in the .m file and have the script look up the corresponding .h file. Locate the protocols supported and so forth...

MacTech ran an article in 2007 Xcode Menu Scripts

1
votes

Xcode 3.2 will autocomplete known method implementations. In other words, if the method is declared somewhere (for example, in a protocol), when you start to type it in a .m file, Xcode 3.2 will autocomplete the method signature for you. This isn't quite what you asked for, but it is awfully handy.

1
votes

I'm also looking for a way to generate method stubs for the protocols in my header file. I checked out Accessorizer and it looks to be a handy tool but unless I missed something I didn't find a way to get it to generate method stubs for a protocol.

Eric, If you found another solution please post what you found. It's amazing to me that XCode doesn't already have this built into the IDE.

1
votes

Since the accepted answer's given link does not work anymore (and is redirected to an ad), here's another good explanation on how to use accessorizer to create protocol method stubs.

0
votes

Based on AllanCraig's "Create @property, @synthesize & dealloc from Variable Declaration" ruby script, I made one to generate implementation stubs from interface ones: http://pastebin.com/4T2LTBh6

How to use?

  • Setup the script on your XCode (Shell Script) and assign a shortcut for it (e.g. CMD+5).
  • Select lines from your interface file in which you want to generate the implementation, and press the hotkey.
  • Your .m will contain your selected methods.
0
votes

I know this is an old question but if you'd like to always see the latest definitions just right click on the class in question and Jump to Definition. Here lyes all the current non-deprecated functions so you aren't relying on a 3rd party to stay up to date.

0
votes

In My case Below style helps me much, In a sense solved my problem.

Suppose you have following method declaration:

+(DBManager*)getSharedInstance;

From Implementation file you start typing +ge and xcode will automatically choose method

+(DBManager*)getSharedInstance;