Objective-C code generators
This is a short collection of Perl scripts that may be useful during Objective-C / Cocoa development to generate accessors and similar methods.
With Objective-C 2.0 and the non-standard properties syntax, there's less need for these scripts nowadays, but I'm leaving this page up for Objective-C 1.0 notalgic out there.
Installation
Typically, one will install these scripts inside ~/Library/Scripts, in order to use them from the Script menu on the Finder. Of course, they all work straight from the Terminal console as well.
Usage
The usage is the same for all these scripts:
- the user copies in the clipboard the member variable declarations for which he wants the accessors, then
- s/he selects the relative script from the Script menu or invoke the script from the shell.
After the script invocation, the generated code is in the Clipboard, so
- the user should execute a "Paste" operation where he wants the generated accessors to be placed.
Each script has usage information included at the beginning of the script code.
Known bugs
- Member variables prefixed by the "IBOutlet", "IBAction" and similar macros are not correctly parsed.
- On Tiger, activating these scripts from the Script menu don't seem to work. They still work on the command line.
Scripts for Accessor Methods
Given some variables declarations, this script generates accessor method declarations for all the specified variables.
Example: for the following variables declarations:
NSString *name; id myObject; float expectedRaise;
The generated code is:
- (NSString *)name; - (void)setName:(NSString *)_t_m_p_; - (id)myObject; - (void)setMyObject:(id)_t_m_p_; - (float)expectedRaise; - (void)setExpectedRaise:(float)_t_m_p_;
Given some variables declarations, this script generates accessor method definitions for all the specified variables. For objects, the "always retain" strategy is used (see example).
Example: for the following variables declarations:
NSString *name; id myObject; float expectedRaise;
The generated code is:
- (NSString *)name { return name; }
- (void)setName:(NSString *)_t_m_p_
{
[_t_m_p_ retain];
[name release];
name = _t_m_p_;
}
- (id)myObject { return myObject; }
- (void)setMyObject:(id)_t_m_p_
{
[_t_m_p_ retain];
[myObject release];
myObject = _t_m_p_;
}
- (float)expectedRaise { return expectedRaise; }
- (void)setExpectedRaise:(float)_t_m_p_ { expectedRaise = _t_m_p_; }
Scripts for NSCoding Methods
Known bugs
Text copied from apps that use Mac style line endings are not correctly parsed by the following 2 scripts. The result is that the methods are generated only for the first variable included in the clipboard text.
genEncodeVarWithCoderPB (v1.2)
Given some variables declarations, this script generates corresponding code for the encodeWithCoder: method of an Objective-C class implementing the NSCoding protocol.
Example: for the following variables declarations:
NSString *name; id myObject; float expectedRaise;
The generated code is:
[coder encodeObject:name forKey:@"name"]; [coder encodeObject:myObject forKey:@"myObject"]; [coder encodeFloat:expectedRaise forKey:@"expectedRaise"];
Note that the generated code includes a TAB character at the beginning of each line: this blends nicely inside a standardly indented Objective-C code.
Given some variables declarations, this script generates corresponding code for the ``initWithCoder:'' method of an Objective-C class implementing the NSCoding protocol.
Example: for the following variables declarations:
NSString *name; id myObject; float expectedRaise;
The generated code is:
[self setName: [coder decodeObjectForKey:@"name"]]; [self setMyObject: [coder decodeObjectForKey:@"myObject"]]; [self setExpectedRaise: [coder decodeFloatForKey:@"expectedRaise"]];
Note that the generated code includes a TAB character at the beginning of each line: this blends nicely inside a standardly indented Objective-C code.