GPMethods is a command line utility for rapidly generating mutators and accessors for Objective-C. Instead of writing dozens of lines of setters, getters, and dealloc statements, GPMethods takes a list of property names and types, and then depending on the flag you give it, will produce the necessary code that you can paste right into your favorite text-editor.
Download the Universal binary by clicking below:

Example
Let’s say that I have the properties name, birthday, age, and favoriteColor. Traditionally, I would have to write code in four places for these properties. The first would be the instance variable in the class’s header file. Next would be the method signatures for the header. Third would be the method signatures and actual implementation for the class’s implementation (.m) file. And finally, I would have to put some code in the class’s dealloc method is it need freeing.But thanks to GPMethods, my life is a little easier. So I can open up Terminal, and to get the instance variable declaration, I would do the following:
gpmethods name NSString\ * birthday NSDate\ * age CGFloat favoriteColor CGColorRef -i
What does this command mean? Well, of course with start with gpmethods as the command name, and then after than we alternate between property names and property types, starting with the name of the property. The end is a flag that tells GPMethods what code we want back (i for instance variable).
Running the command above will place the following code right into the pasteboard:
NSString *_name; NSDate *_birthday; CGFloat _age; CGColorRef _favoriteColor;
And now, I can just paste this right into Xcode, TextMate, BBEdit, whatever.
But, the instance variable declaration is pretty boring, and wouldn’t take much time anyway. But what if I wanted the implementation for the .m file? I would go back to Terminal, and the type the following:
gpmethods name NSString\ * birthday NSDate\ * age CGFloat favoriteColor CGColorRef -m
And this gives me this on the pasteboard:
- (NSString *)name
{
return _name;
}
- (void)setName:(NSString *)newValue
{
if (newValue != _name)
{
[_name release];
_name = [newValue copy];
}
}
- (NSDate *)birthday
{
return _birthday;
}
- (void)setBirthday:(NSDate *)newValue
{
if (newValue != _birthday)
{
[_birthday release];
_birthday = [newValue retain];
}
}
- (CGFloat)age
{
return _age;
}
- (void)setAge:(CGFloat)newValue
{
_age = newValue;
}
- (CGColorRef)favoriteColor
{
return _favoriteColor;
}
- (void)setFavoriteColor:(CGColorRef)newValue
{
if (newValue != _favoriteColor)
{
if (_favoriteColor != NULL) CFRelease(_favoriteColor);
_favoriteColor = (CGColorRef)CFRetain(newValue);
}
}
By looking at the result, I think it’s important to see how really useful GPMethods actually is.
- Generates both mutators and accessors with types and names all in their rightful place.
- GPMethods is smart about how to set properties. If the property is an ObjC object like NSString, it automatically releases it before setting it.
- Objects are not just necessarily retained, but in the case of strings, they are copied so that we can’t screw things up.
- GPMethods handles what kind of object things are. You can see that the CGColorRef is given the correct syntax for dealing with CoreFoundation and CoreGraphics objects, doing CFRelease and CFRetain instead of just release and retain. This is really important because if you ever want to write a garbage collected app on systems with Leopard and beyond, you can’t use release and retain on objects that don’t descend from NSObject, such as CGColorRef.
That is a quick overview of GPMethods, please read the following two sections on installing and then more detailed look at GPMethods.
Downloading Installing
To download, CLICK HERE to download GPMethods as a disk image.
When you open the disk image, there will be two files, one is the actual Mach-O binary gpmethods executable. The other file in there is a shell script called install that you can run, and it will put gpmethods in your /usr/bin folder. When installing, the script runs a sudo cp command, so you will be prompted for your password.
That’s it!
Usage
As I explained above, the call to gpmethods should be done in the following way:gpmethods propertyName1 propertyType1 propertyName2 propertyType2… propertyNameX propertyTypeX -someFlag Where you are alternating between property names and property types. You must have a property type for each name. To do property types that are pointers, such as NSString, do not use quotes, but rather just escape the star with a backslash: gpmethods name NSString\ * -i for example.
The following is the list of flags you can pass in:
- -i: This writes the instance variable declarations to the pasteboard.
- -h: This writes the method signatures for the mutators and accessors for the header file to the pasteboard.
- -m: This writes the method signatures and implementations for the mutators and accessors for the implementation (.m) file to the pasteboard.
- -d: This writes the dealloc statements for any properties whose type requires releasing (except void *) to the pasteboard.
If you pass the -c flag in as the first argument, it will ignore the rest of your arguments and give you the standard comment header, complete with pragma marks. So for example, I might run the command gpmethods -c to get this.
Additional notes on types
As I mentioned above, GPMethods is smart about types. For each type, it looks to see if that type is an object, and if it is, does the following:
- For the implementation flag, it will send a release before setting it, and for the dealloc, it will release it.
- When setting the property value in the mutator, if you have an object such as a string, it will copy it, otherwise, it will retain it.
- void * is not retained, copied, or released for anything. Basically it is just treated as a straight up pointer, and not an object.
- GPMethods knows what kind of object you have, and adjusts its retain and release strategy accordingly. For example, if you have a Cocoa object, such as NSDate, it will use [myDate release] and [myDate retain]. But, if you are using a CoreFoundation type such as CFArray, it will use CFRelease and CFRetain. This even works with CoreGraphics types such as CGColorRef, CGImageRef, CGContextRef, etc. This is very useful so that when you write a garbage collected app, you won’t have to worry about changing releases and retains on CoreFoundation types to use CFRetain or CFRelease.
If you ever need to be quickly refreshed of this content, you can run gpmethods help to get a little help screen.
So why not use Objective-C properties
As I have mentioned numerous times, GPMethods knows about the types you are working with, and can even work with CoreFoundation and CoreGraphics types. Properties can’t do that.