Xcode: Possible to auto-create stubs for methods required by Protocol interface? - objective-c

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?

I believe that Accessorizer will do what you want.

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

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.
#optional methods also available to implement automatically (shortcut: control + I).

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

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.

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.

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.

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.

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.

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;

Related

Header files without implementation

I'm working on a open source project, which consist on a framework for iOS devices, and one of the methods is not working as I expected. I tried to search for the implementation of the method, but all I found was a a header file and the method declaration; I didn't find the implementation anywhere. Neither did I find the .m file corresponding to that class.
So I have some questions:
How can a class exist without it's implementation and still its methods perform certain operations?
What is the purpose of writing this kind of classes.
In this kind of situations where should be the methods implemented?
Note
The open source project is FastPdfKit and the method is wholeTextForPage:
Well, those methods are somewhere, so it's not that they don't exist, you just can't see them.
Try for example to open UITableView.h, you can see the methods definition, but not the implementation. The implementation is hidden in the library, but you can't see it.
In a nutshell, developers do this to hide the details of the implementation of a class to other users. You just receive a header that tells you which methods you can use, and how, but the details about how are they implemented are hidden for you.
For example, Apple doesn't want you to see how they implemented UITableView, but they want you to know how you can use it.
Here you can find a tutorial about how to create a library for Objective-C:
Creating Static Libraries for Objective-C

Force Xcode to jump to implementation and not interface using jump to definition

After I updated to 4.3.3 Xcode started to jump to interfaces instead implementations. Same with open quickly it doesn't show me implementation files, only headers.
Is there same way to fix it?
I don't know of a way to choose whether Xcode jumps directly to either the interface or the implementation.
However, if you are looking at the interface, you can jump to the implementation by command-clicking the name in the interface. If you are looking at the implementation, you can jump to the interface by command-clicking the name in the implementation.
This works for both class names and selectors. Thus you are never more than two command-clicks from both the interface and the implementation (if Xcode knows where to find them).

Objective-C convention for overridden methods

In Java, when you override a method, you are advised (almost forced) to add the #Override annotation. Is there a convention to mark (either in comments, or by some other mechanism) overridden methods in the Objective-C world?
No. All methods in Objective-C are sent via Objective-C's messaging, so all methods can be overridden. That's part of the language.
There's no convention for marking this, either. It's part of the language that it happens, and if you were to comment otherwise it'd just be confusing when you did it later, accidentally or intentionally.
I'm not sure if Xcode does this, but the AppCode IDE from Jetbrains automatically annotates overridden methods with the little blue override badge in the margin, like so:
. . further to that (also shown), I also like to create some live templates (aka code-snippets in Xcode) to annotate overridden methods with a #pragma tag. I find that it helps to define a standard structure in this order:
class methods
initialization & destruction
public methods / protocol methods
overridden methods
private methods
and by having Live Templates/Code Snippets I can just type 'override [tab]' and the IDE will create the #pragma tag for me.
. . perhaps you could even use OCLint to check that this structure is adhered to.
No, not really.
It doesn't seem to matter as much, probably because of the dynamic dispatch.

Auto-generate Objective-C method headers from implementation?

Is there a tool that will take a list of Objective-C methods and produce the corresponding header definitions?
Often when writing code in my implementation file, I find I need to add, remove, or modify method definitions. This requires the tedious (and thoroughly-automatable) step of switching back to my header file and making the exact same changes, twice.
What ever happened to DRY? What kind of tools can I use to make life easier here? Thanks.
You can try Accessorizer:
http://www.kevincallahan.org/software/accessorizer.html
It automates most work regarding properties, it might work for methods too.
Sadly, it's not free.
I don't know of any existing tools (although Interface Builder does allow you to define outlets and actions, and then generate a header and implementation skeleton for you based on those). Remember though that the implementation file can contain information that should not go in the header (such as private methods and instance variables/properties), so it would be difficult for any tool to do this in any case.
For the moment, in Xcode, you can split the window and see both side by side (in Xcode 4, this is the Assistant). Alternatively, you can press Alt-Command-UpArrow to see the corresponding file.

Can one autogenerate a protocols stubs

Still waiting on my MacBook.
When an interface that I create references a protocol e.g. CCLocationManagerDelegate does the various methods get autogenerated or does intellisense pop up with all the available methods to implement?
Xcode doesn't do this unless the methods are in a template file (and I'm not aware of a template for a <CLLocationManagerDelegate>.
That being said, you might want to check out Accessorizer, which is a neat app that can do a whole bunch of code generation for you, of which (I believe) protocol conformance is a part.