Xcode Script For Creating Properties From Instance Variables For UIViewController

One of my favourite blogs on the net is Matt Gallagher’s Cocoa with Love. Today I was investigating ways to speed up my workflow in Xcode and I found a great mixed Perl and AppleScript script already written by Matt a couple of years ago.

A fantastic resource for Cocoa, Objective-C and programming in general

The script called PropertyFromInstanceVariable had already been added to by a couple of other people, Yung-Luen Lan & Mike Schrag and Pierre Bernard and I have now shaped it for my own specific needs, and possibly yours too.

The tedium I was trying to eliminate was the process of creating Objective-C properties from instance variables and doing basic memory management on them. This is a repetitive process that seems more appropriate to hand off to the machine.

Matt’s script allows you to take an instance variable defined in your interface file and automatically create a property for it, synthesize the property in the implementation, add a release statement and set the property to NULL in the dealloc method.

For example, running the script on this:

NSString *myString;

would automatically create this in the interface:

@property (nonatomic, retain) NSString *myString;

this at the top of the implementation:

@synthesize myString;

and adds these two lines to the dealloc method:

[myString release];
myString = NULL;

Because this script was written before the viewDidUnload method had been added to the UIViewController class it didn’t make use of the viewDidUnload method. I wanted to update it to set the retaining references to subviews in the view hierarchy to nil. These reference properties may have been set through IBOutlets when loading a nib or programmatically in the loadView method.

I have now modified the script to reflect my own coding habits. It still creates the property, synthesizes it and adds a release to the dealloc method, but if the class is a subclass of UIViewController then this is placed in viewDidUnload like so:

self.myString = nil;

This script can be run on multiple instance variables at once and greatly cuts down on the time it takes to set up a new class. Instructions on installing the script can be found here.

You can get my fork of the script on github.

NOTE: I have updated to script and edited this post to reflect those changes. All links are to the most recent version.

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Technorati
  • LinkedIn
  • RSS
  • Slashdot
  • Twitter

About Jason Job

I am a technologist, music producer and an obsessive student of the Internet. I create sound, write code and make things with electronics and microcontrollers. I work as a software developer and consultant and am currently focused primarily on mobile applications for iPhone, iPad and iPod Touch.
This entry was posted in OSX, Objective-C, XCode, code, hack, iPhone, script. Bookmark the permalink.

2 Responses to Xcode Script For Creating Properties From Instance Variables For UIViewController

  1. Thanks for this! I received an error messaged related to an Adobe scripting library though the script worked as described. I’ve always thought setting up properties should be easier.

  2. Jason Job says:

    Hmm, not sure why that would happen. I don’t have any Adobe products so can’t help you there. Glad the script worked though. By the way I have updated it to be more universal by first checking if the class is a subclass of UIViewController before adding anything (or creating) the viewDidUnload method. I recommend you download the updated script from the github link at the bottom of the post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>