-
Notifications
You must be signed in to change notification settings - Fork 0
Home
What?
OCHGen is a tool that uses information available from an Objective-C implementation file to do useful things. Currently the useful thing that is performed is the generation of the corresponding header file, although more features are planned in the future. In order to generate the header file, OCHGen uses both information that any .m file would necessarily have, as well as annotations written by the author of the .m file which are comments in a special form.
Why?
I got fed up with cutting and pasting method prototypes as I was evolving class design, and jumping back and forth between .h and .m files to declare properties. As I am a strong believer of Once and Only Once, I thought the development process of Objective-C code could do with a bit of improvement in this regard.
Usage
Write an Objective-C .m file.
Make annotations in the .m file as necessary – see ‘specs’ for what annotations are available, and how the tool interprets them.
Run header_generator.rb HelloWorld.m and observe the output.
When you decide you want to keep using this, define a ‘run script’ build action in XCode that launches the script with your source directory as the argument, to make OCHGen process all your classes and write output if necessary. Here is an example from one of my projects:
ruby -I ~/dev/ruby/OCHGEN ~/dev/ruby/OCHGEN/header_generator.rb "$SRCROOT/Classes"
Spec
-Class-level annotation
Example:
/*@ #import <foundation/foundation.h> state: NSDictionary* keyPathRegistrationDictionary */
Outcome:
Generates import statements and instance variable declarations in the .h file. Content of annotation can be empty. If no class-level annotation found in the .m file, the class is considered out of scope.
-Class name and superclass annotation
Example:
@implementation KVObserver //@ :SomeSuperclass
Outcome:
Generates the interface declaration start line specifying the superclass in the .h file. If no superclass annotation found, defaults to NSObject.
-Property
Example on @synthesize:
@synthesize calcController; //@ (assign,atomic) CalcController*
Outcome:
Generates the instance variable declaration and property declaration in the .h file.
Example on accessor: TODO
-Method prototypes
Example:
-(void) doSomething {
Outcome:
Generates method prototypes in the .h file. No annotation necessary.
More details
See the tests in the tests/ folder which cover all implemented functionality.
Caveats
So far there has been no compelling reason to make OCHGen more sophisticated than the level of mundane automation that it currently achieves. So don’t expect this to shield you from what you would be expected to know about Objective-c .h files, or to cook your dinner.
Ideas for the future
-A ‘private’ section in the implementation, the details of which is generated to a separate private header file.
-Generation of ‘boilerplate’ stuff in the .m file based on information already available – eg. generate default initialiser implementation and dealloc method based on annotations on ivars.
-Generate dealloc method in .m file using state information.
-Macro expansion – e.g. create a formatted string or instiantiate / populate an NSDicitonary using ruby-style syntax.
Please share your ideas if you think there is something useful to be done in a way similar to what has been demonstrated here.